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

Dynamic RTS-style Text UI

Discussion in 'Scripting' started by EvenFarther, Apr 18, 2020.

  1. EvenFarther

    EvenFarther

    Joined:
    Apr 17, 2020
    Posts:
    4
    Hey I'm trying to put text that can change depending on what you do onto the ui of a game that I'm making and for some reason the public variables for the text aren't going onto the inspector of the object that the script is attached to. Any idea why? I'm still new to coding

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UIElements;
    5.  
    6. public class ResourceManager : MonoBehaviour
    7. {
    8.    
    9.     public float gold;
    10.     public float maxGold;
    11.     public float wood;
    12.     public float maxWood;
    13.     public float population;
    14.     public float maxPopulation;
    15.  
    16.     public TextElement goldDisp;
    17.     public TextElement woodDisp;
    18.     public TextElement populationDisp;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.        
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         goldDisp.text = "" + gold + "/" + maxGold;
    30.  
    31.         //if(gold >= maxGold)
    32.         //{
    33.         //    gold = maxGold;
    34.         //}
    35.     }
    36. }
    Untitled.png
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Docs say TextElement is an experimental API. Try Text instead.
     
    eses likes this.
  3. EvenFarther

    EvenFarther

    Joined:
    Apr 17, 2020
    Posts:
    4
    Ok I tried that and it gave me this error Untitled.png

    In case you are wondering, I've tried using .UI instead of .UIElements and after solution after solution .UI only worked in a 2017 unity version I had installed, the 2 2019 versions I have installed, both I've worked on with my game, seems to have taken out .UI and makes me use .UIElements
     
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    What happens if you add "using UnityEngine.UI;" to the top of your script?