Search Unity

Text field of a text UI object in UnityScript?

Discussion in 'UGUI & TextMesh Pro' started by SafariDiscoLion, Aug 22, 2014.

  1. SafariDiscoLion

    SafariDiscoLion

    Joined:
    Jun 28, 2014
    Posts:
    2
    How can I change the text field of a text UI object in a UnityScript? I want to change a text field based on variables from button presses but I can't figure out how, the only answers I found were for C#.
     
  2. T2Unity

    T2Unity

    Joined:
    Aug 21, 2014
    Posts:
    26
    using UnityEngine.UI;

    ~~~~~~~~~~~

    Text textComponent = transform.GetComponent<Text>();
    textComponent.text = "brabra";

    is this correct for you?
     
    Tim-C likes this.
  3. SafariDiscoLion

    SafariDiscoLion

    Joined:
    Jun 28, 2014
    Posts:
    2
    That just gives me a series of errors about semi-colons and parenthesis.

    (3,6)UCE0001: ';' expected. Insert a semicolon at the end.

    (5,5)UCE0001: ';' expected. Insert a semicolon at the end.

    (5,51)BCE0043: Unexpected token: ).

    (5,52): BCE0044: expecting ), found ';'.

    (5,53): UCE0001: ';' expected. Insert a semicolon at the end.
     
    T2Unity likes this.
  4. Lypheus

    Lypheus

    Joined:
    Apr 16, 2010
    Posts:
    664
    I don't believe that is the right approach T2 - actually, there are two things you can do:

    1. You can fetch the top level InputField and set its "value" property directly:
    InputField inputField = myField.GetComponentInChildren<InputField>( );
    inputField.value = "Hello World";

    2. When you add an event from the Event Trigger attached to the button, create a method which takes InputField as a parameter, set that as the target method and set the parameter to the InputField you want to manipulate.
     
    T2Unity and Tim-C like this.