您好,我想创建像飘扬的小鸟中的徽章,我将图像放在UI图像中,例如GameObjects。但是它只显示3张图片中的一张,请帮忙! HighScore可以工作,当我获得更高的分数时,它将重写,但是不知道为什么图像不会改变:(

对不起,英语不好

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{


    private float score = 0f;
    public Text Scoretext;
    public MenuController deathmenu;
    public GameObject IntroGUI, DeathGUI, Canvas;
    public GameObject[] medals;
    public GameObject medale;
    void Start()
    {
        medale.SetActive(false);
        foreach(GameObject m in medals)
        {
            m.SetActive(true);
        }

    }


    void Update()
    {
        //handle back key in Windows Phone
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();

        if (GameStateManager.GameState == GameState.Intro)
        {if (WasTouchedOrClicked())
            {
                GameStateManager.GameState = GameState.Playing;
                IntroGUI.SetActive(false);
                Canvas.SetActive(true);
            }
        }

        else if (GameStateManager.GameState == GameState.Playing)
        {


            score += Time.deltaTime;
            Scoretext.text = ((int)score).ToString();

            if (PlayerPrefs.GetFloat("Highscore") < score)
                PlayerPrefs.SetFloat("Highscore", score);

            if(PlayerPrefs.GetFloat("Scoretext") > 0)
            {
                medals[0].SetActive(true);
            }
            else if
                (PlayerPrefs.GetFloat("Scoretext") > 2)
            {
                medals[1].SetActive(true);
            }
            else if
               (PlayerPrefs.GetFloat("Scoretext") > 5)
            {
                medals[2].SetActive(true);
            }

            medale.SetActive(true);


            deathmenu.ToggleEndMenu(score);
        }

    }


在Image中,您可以看到我的GameObjects和Etc。

c# - Unity High_Score徽章C#-LMLPHP

我更新了代码,但仍然无法正常工作

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class ScoreManager : MonoBehaviour
{


    private float score = 0f;
    public Text Scoretext;
    public MenuController deathmenu;
    public GameObject IntroGUI, DeathGUI, Canvas;
    public GameObject[] medals;
    public GameObject medale;
    void Start()
    {
        medale.SetActive(true);

    }


    void Update()
    {
        //handle back key in Windows Phone
        if (Input.GetKeyDown(KeyCode.Escape))
            Application.Quit();

        if (GameStateManager.GameState == GameState.Intro)
        {
            if (WasTouchedOrClicked())
            {
                GameStateManager.GameState = GameState.Playing;
                IntroGUI.SetActive(false);
                Canvas.SetActive(true);
                medale.SetActive(false);
            }
        }

        else if (GameStateManager.GameState == GameState.Playing)
        {


            score += Time.deltaTime;
            Scoretext.text = ((int)score).ToString();

            if (PlayerPrefs.GetFloat("Highscore") < score)
                PlayerPrefs.SetFloat("Highscore", score);

            deathmenu.ToggleEndMenu(score);


        }
    }




        void FixedUpdate()
    {
            //just jump up and down on intro screen
            if (GameStateManager.GameState == GameState.Intro)
            {

            }
            else if
            (GameStateManager.GameState == GameState.Playing || GameStateManager.GameState == GameState.Dead)
            {
            }
        }

        bool WasTouchedOrClicked()
    {
            if (Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) ||
             (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))
                return true;
            else
                return false;
        }

        void OnCollisionEnter2D(Collision2D col)
    {
            if (GameStateManager.GameState == GameState.Playing)
            {
                if (col.gameObject.tag == "CARS")
                {
                    PlayerDies();
                }
            }
        }




        void PlayerDies()
    {

            GameStateManager.GameState = GameState.Dead;




            if (PlayerPrefs.GetFloat("Scoretext") > 0)
            {
                medals[0].SetActive(true);
            }
            else if
                (PlayerPrefs.GetFloat("Scoretext") > 2)
            {
                medals[1].SetActive(true);
            }
            else if
               (PlayerPrefs.GetFloat("Scoretext") > 5)
            {
                medals[2].SetActive(true);
            }

        }
    }

最佳答案

事实证明,下面的内容不足以让您的内容正常工作,您需要更改Mede.SetActive(false);。赢得奖牌.SetActive(true);因为这是您的奖牌所在的父对象。

您没有在任何地方设置“计分文字”,因此请查看分数而不是“计分文字”。

这样就得到:



score += Time.deltaTime;
            Scoretext.text = ((int)score).ToString();

            if (PlayerPrefs.GetFloat("Highscore") < score)
                PlayerPrefs.SetFloat("Highscore", score);

            if(score > 0)
            {
                medals[0].SetActive(true);
            }
            else if
                (score > 2)
            {
                medals[1].SetActive(true);
            }
            else if
               (score > 5)
            {
                medals[2].SetActive(true);
            }





另外,为什么一开始将它们设置为活动状态?

关于c# - Unity High_Score徽章C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38244923/

10-15 06:50