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. Dismiss Notice

Question How do I set a variable's value as text?

Discussion in 'Scripting' started by Arizen1451yt, Jul 12, 2023.

  1. Arizen1451yt

    Arizen1451yt

    Joined:
    Mar 30, 2023
    Posts:
    5
    I have a variable called textValue attached to a script that I attached to a text object. This Variable's value changes depending on your input, but for some reason, nothing shows up in the text on-screen. How do I make the value of textValue show as text? I have the script attached to the text object, and everything seems to be working. Here's the code:

    public string textValue;

    public Text textElement;

    void Start()
    {
    textElement.text = textValue;
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Well, I'm not seeing anything wrong with the code, so you should make sure it's running and make sure everything in the scene is hooked in correctly.

    Add a Debug.log call into Start to see if it runs. Print out the value of textValue. Check console for errors. Make sure everything in the scene is correctly hooked in. Time to do that debugging!
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,378
    Is this 'textValue' variable changing after 'Start' has ran?

    Note that your script sets 'textElement.text = textValue' ONLY on Start. Not any other time.

    What is it exactly you mean by "changes depending on your input". Is there code associated with that change?
     
    66ALW99 and PraetorBlue like this.
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    On what basis are you making that claim? There are no log statements in your code and you stated that the desired effect is not occurring, so how do you know this code is even running and when?
     
    66ALW99 and Chubzdoomer like this.