Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UI Text not displaying properly.

Discussion in 'UGUI & TextMesh Pro' started by Nitro00, Apr 29, 2018.

  1. Nitro00

    Nitro00

    Joined:
    Jan 11, 2018
    Posts:
    18
    So, I have this text that displays the number of missiles my character holds. It works fine for the start, when you have 1 missiles. However I have it set so that when the player presses a button to try to launch a missile to a spot, it reduces the number of missiles by 1, which gets displayed fine but if the player decides not to launch it, the number reverts back to whatever it was before the subtraction. This does not get shown on the UI.

    Any help?
    C# is preferred.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine.UI;
    4. using UnityEngine;
    5.  
    6. public class RocketCount : MonoBehaviour {
    7.  
    8.     public Text rocketCount;
    9.     GameObject player;
    10.     int missilesCount;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.  
    15.         player = GameObject.FindGameObjectWithTag ("Player");
    16.         missilesCount = player.GetComponent<PlayerController> ().missiles;
    17.  
    18.     }
    19.    
    20.     // Update is called once per frame
    21.     void Update () {
    22.  
    23.         missilesCount = player.GetComponent<PlayerController> ().missiles;
    24.         rocketCount.text = "X " + missilesCount.ToString ();
    25.  
    26.     }
    27. }
    28.  
     
  2. Shack_Man

    Shack_Man

    Joined:
    Jun 7, 2017
    Posts:
    367
    I don't see why rocketcount.text would display a wrong number, maybe post the part of your player script?

    On a side note, I wouldn't use get component in the update function. An easier and more efficient way would be to reference the rocketcount in your player script. And your probably don't need to put that in an update function either, just update it when you change the "missiles".
     
  3. Nitro00

    Nitro00

    Joined:
    Jan 11, 2018
    Posts:
    18
    It isn't displaying the wrong number exactly, it just doesn't update properly. Also, I feel like in another script, when the text is changed, it doesn't get rid of the parts before it, just adds layers of that number on and on, so I feel like the text UI is just buggy :/
     
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,500
    Before submitting as a bug, try to use Debug.Log to print and display in the console the value.
    Also, an OnGUI helps to display and debug the value on screen:

    Code (CSharp):
    1. void OnGUI()
    2. {
    3.     GUILayout.Label ( missilesCount +  " = " + missilesCount.ToString() );
    4. }