Unity High_Score Badges C#

二次信任 提交于 2019-12-10 22:20:33

问题


Hello I want to create badges like in flappy bird , I put images in UI images like GameObjects. But it show me only one image of 3 images Please Please Help Out! And HighScore works , it rewrites when I get Higher score , but don't know why images won't change :(

Sorry For bad english

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);
        }

    }

In Image you can see my GameObjects and Etc.

I updated the code but still doesnt work

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);
            }

        }
    }

回答1:


Turns out the stuff below is not enough to let your stuff work, you need to change medale.SetActive(false); to medale.SetActive(true); because this is the parent object which your medals are in.

You don't set the Scoretext anywhere so look at the score instead of the scoretext.

So you get:

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);
            }

Also, why are you setting them active at the start?



来源:https://stackoverflow.com/questions/38244923/unity-high-score-badges-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!