Search Unity

Change UI button text from InputField on another scene

Discussion in 'UGUI & TextMesh Pro' started by Novalis, Jan 16, 2015.

  1. Novalis

    Novalis

    Joined:
    Oct 9, 2013
    Posts:
    2
    Hi. I am trying to change the text on a button from the text entered on an InputField in another scene. Here's the code I am using, calling createAccountName() when a button in the same scene with InputField is clicked:

    Code (CSharp):
    1.  public class CreateNewAccount : MonoBehaviour {
    2.          
    3.                  public InputField enterAccountName;
    4.                  public GameObject aObject;
    5.                  public Button aButton;
    6.  
    7.                 public void createAccountName () {
    8.                        aObject = GameObject.Find ("User2"); //The button name is User2
    9.                        aButton = aObject.GetComponent<Button> ();
    10.                        aButton.GetComponentInChildren<Text>().text = enterAccountName.textComponent.text;
    11.                 }
    12. }
    I get a Exception "Object reference not set to an instance of an object". What am I doing wrong? Thank you.
     
    royceluo20 likes this.
  2. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Could be many things. What line does it tell you the error is at?

    Also use the .text not the .textcimponent.text
     
  3. Novalis

    Novalis

    Joined:
    Oct 9, 2013
    Posts:
    2
    I get the exception in line 9, aButton = aObject.GetComponent<Button> (); , I suspect that the button is not an object, but cannot think of a way to get it. The ."textcimponent.text" is working when I test it with a button in the same scene, the problem is how to access a button in another scene. Thanks.