- Home /
Accesing the text component of a button on Unity 4.6
Hi. I am trying to find and change the text (coming from an InputField) of a button that is in another scene that the InputField. I am using:
public InputField enterAccountName;
public GameObject aObject;
public Button aButton;
public void changeText() {
aObject = GameObject.Find ("buttonName");
aButton = aObject.GetComponent<Button> ();
aButton.GetComponentInChildren<Text>().text = enterAccountName.textComponent.text;
}
but I am getting an exception. What am I doing wrong?
What exception are you getting, is your find finding anything? do Debug.Log(aObject); just after the find to see what its set to.
I get a Exception "Object reference not set to an instance of an object" from "aButton = aObject.GetComponent ();". I can use the same code successfully to change the text in a button in the same scene, but cannot seem to be able to find a button in another scene. I have used also FindGameObjectWithTag to search for the button without success.
Debug.Log(aObject) right after aObject = GameObject.Find ("User2"); gives me "Null".
For testing try this:
aButton = aObject.GetComponentInChildren<Button>();
Or, as you've go them set to public just drag the button you reference onto the slot for it in the inspector.
Thanks for the help. The problem seems to be before that, because of the Null object I get from "aObject = GameObject.Find ("User2");". "User2" is the name of the button that is found in another scene, so I cannot drag it in the inspector. I have tested with a button in the same scene and it works.
I cannot understand what I am doing wrong. 'User2" is the name of the button as shown in the hierarchy. That's what I should use, right?
Answer by Mmmpies · Jan 17, 2015 at 07:31 PM
The script has to be in the same scene as the button. You might need a static variable that the other button can pick up when that scene opens but if you're going to use this method a lot or for a lot of buttons avoid it as it's really bad practice (but OK for one off things).
If your player is a dontdestroyonload you could store the variable in a setting for the player and again pick up the variable when the other scene loads.
There are ways of doing it but you can't just assign a value to a scene that's not in memory.
Thank you for your answer. Didn't knew that I couldn't access a button that is not in the same scene. I guess I would go with the static method.
Thanks for the accept @Novalis but do try and limit static as much as possible, it really is bad practice. I only use it to store which scene I've come from and even then that's a legacy from when I knew nothing at all about unity or C#.
Like I said you're O$$anonymous$$ using it in a very limited way.
You're using it to store enterAccountName by the looks of things, if that's all you need to store from one scene to the next then that's fine. If you start using static for other things consider having a script that remains intact in each scene like a gameSettings type script that uses "DontDestroyOnLoad (this);" to store constant settings.
Always assu$$anonymous$$g you're making a game! enterAccountName could mean you're creating a banking system and decided using a 3D/2D game engine was the way to go. If so big thumbs up for individual thinking but massive thumbs down for your potential involvement in the global financial meltdown of recent times ;¬)
Hi. Actually I am making a virtual piano prototype application for a university course I am taking, it involves also some program$$anonymous$$g with the Leap $$anonymous$$otion Controller (which I have no clue how to proceed atm). The enterAccountName refers to one of the four possible account that users can create to supposedly take some piano lessons. Nothing to do with banks and all that :)
Answer by alok-kr-029 · Jan 17, 2015 at 03:22 PM
using UnityEngine.UI;
Text aButton = aObject.GetComponentInChildren(); aButton.text = "Your String";
Hope this will help you aObject is your button
Thank you for the reply. The problem seems to be that I cannot get the (object) button "User2" that is on another scene with "Find" for some reason. Below is all the code. I have an "InputField" and an "Accept" button in the same scene. I want to change the text of the button "User2" in another scene. I drag the InputField "enterAccountName" in the Inspector. I call "createAccountName" via "On Click()" from the "Accept" button.
using UnityEngine.UI;
public class CreateNewAccount : $$anonymous$$onoBehaviour {
public InputField enterAccountName;
public GameObject aObject;
public Button aButton;
public void createAccountName () {
aObject = GameObject.Find ("User2"); //The name of the button in Hierarchy
Debug.Log(aObject); //Gives Null
aButton = aObject.GetComponentInChildren<Button> ();
aButton.GetComponentInChildren<Text>().text = enterAccountName.textComponent.text;
}
}
Can you please check if this code works for you? Thank you.
Strange thing is that GameObject.Find("buttonName") will return a button that is in the same scene, but not a button in another scene. Is there any other way to search for a button on another scene?
Other scenes don't exist until they're loaded; you can't interact with them in any way. Sounds like you need to re-think the structure of your project.
Answer by farukaf · Nov 25, 2016 at 07:57 AM
So I make a twitch where I solve the problem.
The unity seems to crash when put all stuff on OnValueChange method so I make a work around. That is the code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextFieldFilter : MonoBehaviour
{
public Button[] Btns;
public Text[] TxtBtns;
Vector3[] btnPos;
string filter = "";
// Use this for initialization
void Start()
{
btnPos = new Vector3[Btns.Length];
for (int i = 0; i < Btns.Length; i++)
{
btnPos[i] = Btns[i].gameObject.transform.position;
}
}
// Update is called once per frame
void Update()
{
if (filter != "")
{
for (int i = 0; i < TxtBtns.Length; i++)
{
for (int j = 0; j < filter.Length; j++)
{
if (filter[j] != TxtBtns[i].text[j])
{
//Debug.Log(filter[j] + " " + TxtBtns[i].text[j]);
Btns[i].gameObject.SetActive(false);
}
else
{
Btns[i].gameObject.SetActive(true);
}
}
}
}
else
{
for (int i = 0; i < TxtBtns.Length; i++)
{
Btns[i].gameObject.SetActive(true);
}
}
}
public void HideBtns(string field)
{
filter = field;
}
}
Just put them on the right place on editor and should work.