Home > Net >  Unity UI Text Value Only Updates In Editor Game Mode
Unity UI Text Value Only Updates In Editor Game Mode

Time:03-28

I am making a 2D game in Unity and am using a Text element to tell players whose turn it is and to keep track of the GameState and player turns. In the Unity game mode the text value is changed to the expected value but this is not the case for the actual build. These values are described later down in the post with images.

I have spent the last 2 days reading through forums and praying for a fix through trial and error but this has not been successful. This UI element used to work perfectly fine and I am not receiving any NullReference errors which led me to believe it was a Unity issue so I have tried:

  • Updating my editor version
  • Using TextMesh Pro Text
  • Re-adding my menu manager script
  • Changing the text value in different ways instead of GetChildrenInComponent
  • Disabling features I had added since this UI feature
  • Messing around with my hierarchy

This is my MenuManager Script. This question mainly refers to my text element which is a child of the public GameObject turnInfo variable and the ShowTurnInfo() function called in Update() located near the bottom of the script.

public class MenuManager : MonoBehaviour
{
    public static MenuManager Instance;
    public GameObject tileInfo, tileUnit, turnInfo;
    public TMP_Text name1, name2, name3, name4, name5;
    public GameObject border1, border2, border3, border4, border5;
    

    void Awake() {
        Instance = this;
        SetUIPlayerNames();
    }

    public void SetUIPlayerNames() {

        if (PhotonNetwork.CurrentRoom != null)
        {

            ArrayList names = new ArrayList();

            foreach (Photon.Realtime.Player p in PhotonNetwork.PlayerList)
            {
                names.Add(p.NickName);
            }


            name1.text = (string)names[0];
            name2.text = (string)names[1];
            name3.text = (string)names[2];
            name4.text = (string)names[3];

            if (PhotonNetwork.CurrentRoom.PlayerCount > 4)
            {
                name5.gameObject.SetActive(true);
                name5.text = (string)names[4];
            }
        }
    }

    //UI element to show the name of the biome and unit in a specific tile
    public void ShowTileInfo(Tile tile) {

        if (PhotonNetwork.CurrentRoom == null)
        {
            tileInfo.GetComponentInChildren<Text>().text = "Offline";
            tileUnit.GetComponentInChildren<Text>().text = "Offline";
            return;
        }

        if (tile == null) {
            tileInfo.SetActive(false);
            tileUnit.SetActive(false);
        }
        
        
        tileInfo.GetComponentInChildren<Text>().text = tile.tileName;
        tileInfo.SetActive(true);

        if (tile.OccupiedUnit) {
        tileUnit.GetComponentInChildren<Text>().text = tile.OccupiedUnit.UnitName;
        tileUnit.SetActive(true);            
        }
    }

    void Update()
    {
        ShowTurnInfo();
    }


    //UI element stating which faction's turn it is
    public void ShowTurnInfo() {

        if (!PhotonNetwork.IsConnected)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Offline";
        }                    
        else if (GameManager.Instance.GameState == GameState.HumanTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Human";
            border1.SetActive(true);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.OrcTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Orc";
            border1.SetActive(false);
            border2.SetActive(true);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.ElfTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Elf";
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(true);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.DemonTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Demon";
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(true);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.DwarfTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Dwarf";
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(true);
        }
        else if (GameManager.Instance.GameState == GameState.EndGame)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Game End";
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else
        {
            turnInfo.GetComponentInChildren<Text>().text = "Waiting";
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        
    }
    

    /*UI element to show a win or loss for battles
    public void ShowBattleWin() {
        loseText.gameObject.SetActive(false);
        winText.gameObject.SetActive(true);
    }

    public void ShowBattleLoss() {
        winText.gameObject.SetActive(false);
        loseText.gameObject.SetActive(true);
    }
    */

}

Editor Game Mode

This image shows the text value updates to "Offline" from "player turn" as expected.

Actual Build

This image shows the text value is not updated in the build and keeps its default value assigned in the inspector.

Hierarchy

This is my hierarchy where the text value is a child of TurnInfo and this is a child of LeftItems which represents UI that appears on the left side of the screen

CodePudding user response:

I have now fixed this issue. The steps I took were:

  1. Deleted and recreated my TurnInfo UI. The only difference I made was not setting an initial Text value through the inspector.
  2. Transferred my code relating to turnInfo and put it in a separate script called TurnManager and attatched this to another empty GameObject.
  3. I set an initial value for the Text in Start() and separated some of my code.

This is how my script looks now:

using Photon.Pun;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TurnManager : MonoBehaviour
{

    public GameObject border1, border2, border3, border4, border5;
    public GameObject turnInfo;

    private void Awake()
    {
        
    }

    private void Start()
    {
        turnInfo.GetComponentInChildren<Text>().text = "Player Turn";
    }
    private void Update()
    {
        ShowTurnInfo();
        ShowTurnBorders();
    }

    public void ShowTurnInfo()
    {
        if (PhotonNetwork.CurrentRoom == null)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Offline";
        }
        else if (GameManager.Instance.GameState == GameState.HumanTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Human";
        }
        else if (GameManager.Instance.GameState == GameState.OrcTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Orc";
        }
        else if (GameManager.Instance.GameState == GameState.ElfTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Elf";
        }
        else if (GameManager.Instance.GameState == GameState.DemonTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Demon";
        }
        else if (GameManager.Instance.GameState == GameState.DwarfTurn)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Dwarf";
        }
        else if (GameManager.Instance.GameState == GameState.EndGame)
        {
            turnInfo.GetComponentInChildren<Text>().text = "Game End";
        }
        else
        {
            turnInfo.GetComponentInChildren<Text>().text = "Waiting";
        }
    }

    public void ShowTurnBorders()
    {
        
        if (GameManager.Instance.GameState == GameState.HumanTurn)
        {
            border1.SetActive(true);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.OrcTurn)
        {
            border1.SetActive(false);
            border2.SetActive(true);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.ElfTurn)
        {
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(true);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.DemonTurn)
        { 
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(true);
            border5.SetActive(false);
        }
        else if (GameManager.Instance.GameState == GameState.DwarfTurn)
        {
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(true);
        }
        else if (GameManager.Instance.GameState == GameState.EndGame)
        {
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
        else
        {
            border1.SetActive(false);
            border2.SetActive(false);
            border3.SetActive(false);
            border4.SetActive(false);
            border5.SetActive(false);
        }
    }
}
  • Related