- Home /
Accessing a certain prefab and it's children
I have a list of items that is used to instantiate a list of button prefabs. Each of this buttons has a Text child that displays the name of the item attached to it/the item that spawned it.
I tried to attach a Script to these buttons that displays the values of the item the button referes to in another Panel. I tried to do it with a loop that searches through the item list and stops when the textdisplay on the button is equal to the item name. This however always returned the values of the first item in the list. Via Debugging I figured out that the script doesn't compares the display text on the button that was clicked but all display texts on all prefab clone buttons. So I obviously need a way to specify that I only want to compare the text on the button that was clicked and not all of them. I spent the last three hours googling for a solution and tried out different things, but nothing worked so far.
The most recent version looks like this:
private void OnMouseDown()
{
SetWriterData(gameObject.GetComponentInChildren<Text>());
}
public void SetWriterData(Text currentname)
{
for (int i = writerlist.Count - 1; i >= 0; i--)
{
if (currentname.text == writerlist[i].name)
{
Name.text = writerlist[i].name;
Intel.text = writerlist[i].intelskill.ToString();
Complex.text = writerlist[i].complexskill.ToString();
Pace.text = writerlist[i].paceskill.ToString();
Dialogue.text = writerlist[i].paceskill.ToString();
Humor.text = writerlist[i].humorskill.ToString();
}
}
}
How can I solve this problem? Is what I'm trying to do possible? Is there a better way to do it?
public static List<$$anonymous$$ovieClass.Writer> writerlist = new List<$$anonymous$$ovieClass.Writer>();
public GameObject WriterDisplay;
public Transform WriterDisplayPanel;
public Text WriterNameText;
public Text Name;
public Text Intel;
public Text Complex;
public Text Pace;
public Text Dialogue;
public Text Humor;
public void GetWriterList()
{
StreamReader reader = new StreamReader("C:\\Users\\magda\\Desktop\\FilmStudioTycoon\\People\\Writer.txt");
string s = reader.ReadLine();
while (s != null)
{
char[] delimiter = { ',' };
string[] fields = s.Split(delimiter);
writerlist.Add(new $$anonymous$$ovieClass.Writer(fields[0], int.Parse(fields[1]), int.Parse(fields[2]), int.Parse(fields[3]), int.Parse(fields[4]), int.Parse(fields[5]), int.Parse(fields[6])));
s = reader.ReadLine();
}
}
void SetWriterList()
{
foreach ($$anonymous$$ovieClass.Writer writer in writerlist)
{
GameObject NewWriterDisplay = Instantiate(WriterDisplay);
NewWriterDisplay.transform.SetParent(WriterDisplayPanel);
WriterNameText.text = writer.name;
}
}
Answer by eskivor · Jul 21, 2017 at 11:53 PM
If I understand you well, you want to only access to the button clone that you have spawned and not directly to the button prefab itself (whose the text is the same for all), that's right ?
To access only to the clone that spawned, you have to create a reference of it first when you spawn the prefab on the scene.
To do that, when you have declared your prefab GameObject variable like this :
//Here's the variable you have linked to the prefab in the inspector
public Gameobject buttonPrefab;
when you instantiate the prefab do that :
GameObject buttonClone = Instantiate (buttonPrefab) as GameObject;
instead of just
Instantiate (buttonPrefab);
then re-use your reference to access to the component you want, for example :
Text buttonCloneText = buttonClone.GetComponent<Text>();
if (buttonCloneText != null)
{
//Do something here
buttonCloneText.text = "I'm a button clone !";
}
Text buttonCloneText = buttonClone.GetComponent<Text>();
This line is not working for me. On it's own it won't compile because "the name "buttonClone" doesn't exist". When I put GameObject buttonClone in the beginning of the script, it complies but Unity throws an error NullReferenceException: Object reference not set to an instance of an object.
I guess the problem is, that the Clone is created in a diffrent function and only exists there?
For the variable GameObject buttonClone
, to re-use it in another method you can :
Either declare it as a global variable like this :
publiic class $$anonymous$$yClass : $$anonymous$$onoBehaviour
{
GameObject buttonClone;
void $$anonymous$$ethod1 ()
{
buttonClone = null;
Debug.Log ("buttonClone : " + buttonClone);
}
}
Either declare it as a local variable, in a method and send it as a parameter when you use another method, like this :
publiic class $$anonymous$$yClass : $$anonymous$$onoBehaviour
{
void $$anonymous$$ethod1 ()
{
GameObject buttonClone = null;
$$anonymous$$ethod2 (buttonClone);
}
void $$anonymous$$ethod2 (GameObject _buttonClone)
{
Debug.Log ("buttonClone : " + _buttonClone);
}
}
And if you have a null reference exception, it's because you are trying to access to an object which is null. You can use a if (nameofYourObjectVariable != null) {}
to avoid the error, ins$$anonymous$$d it will just do nothing, to execute the method well, check before that your object is not null (here I suppose that you didn't assign your buttonPrefab via the inspector).
Your answer
![](https://koobas.hobune.stream/wayback/20220612130830im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Display text above prefabs in Unity 4.6 0 Answers
Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers
Dynamically assign each ui.Text.text in list to the name of objects in another list 2 Answers
Is constantly referencing or defining once then changing better 1 Answer
Dictionary Keys to Stack Enemy Types 1 Answer