Change UI button text from InputField on another scene

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:

 public class CreateNewAccount : MonoBehaviour {
          
                 public InputField enterAccountName;
                 public GameObject aObject;
                 public Button aButton;

                public void createAccountName () {
                       aObject = GameObject.Find ("User2"); //The button name is User2
                       aButton = aObject.GetComponent<Button> ();
                       aButton.GetComponentInChildren<Text>().text = enterAccountName.textComponent.text;
                }
}

I get a Exception “Object reference not set to an instance of an object”. What am I doing wrong? Thank you.

1 Like

Could be many things. What line does it tell you the error is at?

Also use the .text not the .textcimponent.text

I get the exception in line 9, aButton = aObject.GetComponent (); , 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.