I’m trying to create multiple instances of a prefab and as each one is created I change a string variable on the new instance. However, I only ever manage to change the variable on the first instance. I can’t figure out where I’m going wrong. Here’s the prefab layout:
Table (Parent not in prefab)
ServerEntry (empty game object container)
Label (gameobject with UILabel script I need to change text on)
I use this (mostly copied from nguitools script) to do the instantiating. I tried my own code first but it was doing the same thing so i tried this
var go : GameObject=GameObject.Instantiate(entryPrefab) as GameObject;
var t : Transform = go.transform;
t.parent = tableObject.transform;
t.localPosition = Vector3.zero;
t.localRotation = Quaternion.identity;
t.localScale = Vector3.one;
go.layer = tableObject.layer;
labelObject = go.Find("LabelTitle");
labelObject.GetComponent(UILabel).text=newServer.name;
I thought that since I was using “go” and “go” would be the instance reference then go.Find (also tried other search/find methods) that I’d get the script instance attached to the newly created instance but it seems I always get the first instance.
Thoroughly exhausted my brain on this
Actually I think I figured it out during the drive to work. GetComponentInChildren()
I remembered reading this in the docs a week or so ago and wondering how you were supposed to get a child component when the argument is only a scriptname and no parent object. It was after that where I figured out you use the parent object to call GetComponent etc.
So my code above starts its search at the highest point in the heirarchy and runs across the first instance and returns its script. I was thinking that calling from the instance’s reference would limit the search to below it’s place in the heirarchy. Made sense at the time but I guess that would be too limiting.
what happens if you try this?
go.Find("/table/serverEntry/label")
(you’ll have to tweak the names to what you’ve used!)
Not quite what I need as there are several instances of “ServerEntry” all with a child “label”. I tried GetComponentInChildren and saw no change in behavior (still only the first instance is affected).
I think I need to change the go.Find(“LabelTitle”) because it appears to be searching the entire hierarchy instead of just starting the with returned gameobject and the only reason I see the first instance changing is because it happens to be the first “LableTitle” in the heirarchy. If I had another object named that higher up it would have been changed instead.
I’ve been searching the docs for a GameObject equivilent to GetCompnentInChildren() but I can’t seem to find one.
if you have a deep hierarchy, you could also attach a simple script to the top one with a public var set to the “label” transform. That way you only have to access that top script to retrieve the target.
I got it!
First error I was making was that I didn’t realize capitalization meant the difference between a property and a type declaration. I thought Transform was both property and type declarion so whenever I tried to access a transform I was getting errors. Paying much closer attention to that now.
Anyway. I already had the top level object in the instance so all I really needed was to search below that for the child. Once I got the capitalization sorted this actually worked:
go.transform.FindChild(“LabelTitle”).GetComponent(UILabel).text = newServer.name;