简体   繁体   中英

Unity UI is not updating

I have a canvas with three text objects in unity. i have 4 scenes: Load, Play, Hub, and Tutorial. I'm using a Script Object that keeps track of your scores as you play. But, every time I set a high score, my text objects still say "1st" "2nd" "3rd." Here's my script: (Also, in play, the text for my score updates, but the (Highscores) is not updating between scenes. And the object the script is on is parenting all except for Camera.Main)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class TrackScores : MonoBehaviour
{
    public Transform First;
    public Transform Second;
    public Transform Third;
    public Text Best;
    public Text Average;
    public Text Worst;
    public Text Score;
    public Transform ScoreTracker;
    public Canvas CanvUI;
    public float ScoreInt;
    public Vector3 PosPlayer;
    void Start()
    {
        DontDestroyOnLoad(Camera.main.transform.root.gameObject);
        DontDestroyOnLoad(transform.root.gameObject);
        SceneManager.LoadScene("Hub", LoadSceneMode.Single);
    }
    void Update()
    {
        if(SceneManager.GetActiveScene().name == "Play")
        {
            PosPlayer = new Vector3(GameObject.Find("Player").transform.position.x, GameObject.Find("Player").transform.position.y, 0);
            Camera.main.transform.position = PosPlayer + new Vector3(0, 0, -15);
            transform.position = new Vector3(0, 0, 1001);
            Score = GameObject.Find("Score").GetComponent<Text>();
            ScoreTracker = GameObject.Find("ScoreTracker").transform;
            ScoreInt = ScoreTracker.position.x * 1000;
            Score.text = ScoreInt.ToString("0");
        }
        if(SceneManager.GetActiveScene().name == "Tutorial")
        {
            PosPlayer = new Vector3(GameObject.Find("Quad").transform.position.x, GameObject.Find("Quad").transform.position.y, 0);
            Camera.main.transform.position = PosPlayer + new Vector3(0, 0, -15);
            transform.position = new Vector3(0, 0, -1000);
        }
        if(SceneManager.GetActiveScene().name == "Hub")
        {
            Camera.main.transform.position = new Vector3(0, 0, -25);
            transform.position = new Vector3(0, 0, 0);
            Best.text = "1st: " + First.position.x.ToString("0");
            Average.text = "2nd: " + Second.position.x.ToString("0");
            Worst.text = "3rd:" + Third.position.x.ToString("0");
            if(ScoreInt < Second.position.x && ScoreInt > Third.position.x)
            {
                Third.position = new Vector3(ScoreInt, 0, 0);
            }
            if(ScoreInt < First.position.x && ScoreInt > Second.position.x)
            {
                Second.position = new Vector3(ScoreInt, 0, 0);
            }
            if(ScoreInt > First.position.x)
            {
                First.position = new Vector3(ScoreInt, 0, 0);
            }
        }
    }
}

Any help is appreciated!

I figured it out. i just need to use text mesh with TextMesh UI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM