Search Unity

need a hand changing text on a textfield

Discussion in 'Scripting' started by tim76, Jun 25, 2017.

  1. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    Ok I have cube, under the cube a canvas, with a panel, then empty game object called a layout, follow by an inputfeild, and then finally Text... this is a menu for change vars on the cube, and so it looks like this.

    Cube
    >Canvas
    >>Panel
    >>>Layout
    >>>>InputField
    >>>>>Text1

    I have script at the cube to change the Text1 and it looks like this...

    function Updatemenu () {

    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2); //gets inputfield in child of cube (this works)

    var Vinput1 = Vinput.transform.Find("Text1"); //get the Text1 of the inputfield (this works)

    Debug.Log(Vinput1.name); //looks to see what i got, and got I the Text1 (this works)

    //something wrong after this point.

    var texttochange = Vinput1.GetComponent(UI.Text); // Grab the Text (script) inside Text1
    texttochange.text = "123";
    //change the text (no errors but nothing happens)
    }

    Now this complies and runs fine within Unity, but it doesn't actually change the text?????
    Any ideas???
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Inputfields and text box that is connected to it works a bit different. You don't access the text box itself, but the text on the inputfields.

    So, in your case. Vinput.Text is what you need to get or set to change what is shown in the textbox itself (the visual component)
     
  3. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    well I tried this first...

    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2); //get inputfield in child
    Debug.Log(Vinput.name); // this works and get the name of InputField
    Debug.Log(Vinput.Text); // this errors out.


    and Unity will not compile it, I am getting an error message...

    Assets/Jscript/RightClickMenu.js(28,18): BCE0019: 'Text' is not a member of 'UnityEngine.Transform'.
     
  4. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    the line above that reads...

    Debug.Log(Vinput.name);

    Outputs to the console:

    InputField
    UnityEngine.Debug:Log(Object)

    So I know I got the Inputfield and '.text' should be a property of it so I have not clue why it's not working.
     
  5. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    below is my javascript, maybe is bug in unity with JS, can someone rewrite this in C# and let me try it...

    import UnityEngine.UI;
    import UnityEngine;
    import System.Collections;

    #pragma strict


    function Start () {

    }

    function Update () {

    if(Input.GetMouseButtonDown(1)) {
    var meeple = this.gameObject.transform.GetChild(0);
    meeple.gameObject.SetActive(true);
    var ChildComp = meeple.GetComponent(closeme);
    ChildComp.hideme = true;
    Updatemenu ();
    }

    }


    function Updatemenu () {

    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2); //get inputfield in child
    Debug.Log(Vinput.text); // is the only thing not working

    }
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    It's not working because it says it's a transform and not an inputfield. The error tells you exactly what is wrong.

    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2).
    GetComponent.<InputField>();

    I think that's the correct way to get a component using unityscript. Otherwise, you'll have to figure that out yourself.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Code (csharp):
    1.  
    2. public InputField myInputField; // drag & drop in the inspector
    3.  
    4. public void UpdateMenu() {
    5.    myInputField.text = "abra kadabra";
    6.   }
    7.  
    :).
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yep, you can do this as well of course. Just trying to help out with the code the way they had it. :p

    (and this is the better way to go, but also note that this is written in c# and not in unityscript which they are using)
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I know, I know.. For sure :) It's good to know how to get there. Maybe the design is dynamic or something.

    Plus, I already saw that you helped him solve it.. So, it was just fun to add that :)
     
  10. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    It's not working because it says it's a transform and not an inputfield. The error tells you exactly what is wrong.....

    I don't know in unity what the difference between a transform and other objects, I am new to unity...anybody know of a good book for reference? Maybe something to tell me what object are available in Unity and what there properties are.
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
  12. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    So...
    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2).


    I am using Javascript, and can I switch to C# no prob, but I still trying understand what I did wrong here.
    I know from doing so reading that the (gameOject.transform) and that transform is a position. So I see that wrong.

    I am currently at work, and will play around with this when I get to home computer later, but what should I have done to get this component. I could have a few different InputFields under this cube, so I used "GetChild(0).GetChild(0).GetChild(0).GetChild(2)" to get down to one I wanted.

    looking order of components from the 1st post, what code would you suggest to dill down and capture the correct inputfield.
     
  13. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    My bad...

    var Vinput = this.gameObject.transform.GetChild(0).GetChild(0).GetChild(0).GetChild(2).
    GetComponent.<InputField>();

    I didn't notice that the ; moved down to second line, and that this is one line of code. So this is the answer...lol
     
  14. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Yep! It's a long line of code. lol.

    GetChild returns a transform, which is why you have to GetComponent to get the inputfield.

    I think the suggestion for c# is mostly as a newer Unity user, there are more examples in c# these days, especially with all the new stuff Unity is adding. (So it might be easier to learn with c#)
     
  15. tim76

    tim76

    Joined:
    Nov 11, 2014
    Posts:
    20
    I like Javascript, it just seem less code most the time. With that said, I think this line of code would be almost the same in both Javascript and C# , but yeah is long code line. Couldn't think of a better way to go down in the hierarchy to get the child I wanted. Probably would have been a better idea to give the infield a name, but I making this a prefab and trying to keep it pretty generic.