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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

[solved]accessing text boxes' text field in code

Discussion in 'UGUI & TextMesh Pro' started by gibberingmouther, Jan 21, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    here's what i tried, with the help of someone on IRC: https://hastebin.com/iferuvezoc.cs

    i dragged the script into the text box, and then dragged the text box into the myText field. unfortunately, no numbers are displayed in the text field when i run the game and press the buttons.

    can i use get component somehow? what should i do? or can i get the text field to work somehow? if you need to see more code i can show you my AbilityPointbuySet script but this shouldn't be necessary.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    6,990
    I'd say double check that the methods are being run (insert debug.log with the Globals.abilities[1].ToString("G") if needed to see if you are getting what you expect) and then if it is, select the item in the inspector to see if the text is even being added to the text field. If you see the text, make sure your textbox is big enough to show the text, else it isn't going to display.
     
  3. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    so what i described doing, dragging the script into the text box, and then the text box itself into the myText field - that should work? i don't need to do something with get component or another method?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    6,990
    If you add a Text object into your scene. It should be under a canvas( I assume you have this since you said you had buttons). The script you showed can be anywhere since you are adding the text box into the variable by dragging and dropping it, then you are fine. There is no need to use getComponent since you already have the reference to the text box through the drag and drop in inspector.
     
  5. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    https://hastebin.com/bumayovefo.cs

    what's wrong with this code? i associated the buttons (up and down buttons for each attribute) with the appropriate methods and then i dragged the appropriate text box into the text field for each text box. still not working....

    edit: got it working, sort of! i think there are just some logical errors making my ability setting gui not work completely right. that's easy to fix though.

    i used:
    Text text;
    text = GameObject.Find("Text (4)").GetComponent<Text>();
    text.text = Globals.abilities[0].ToString("G");

    you get the idea. hope this helps someone in the future get unstuck.
     
    Last edited: Jan 22, 2017