- Home /
Can't set script value on child of instanced gameobject
To start, I've been successfully instantizing objects and accessing variables in the scripts of these instances to modify upon creation as shown below:
GameObject newObject = Instantiate (Object, transform.position, transform.rotation) as GameObject;
ScriptName newScriptNameComp = newObject.GetComponent<ScriptName>();
newScriptNameComp.variableName = someValue.transform;
Where I'm getting stuck is when I have a prefab I instance, and in that prefab there are objects with scripts which I want to send values as well.
For example (using above code):
newObject.childObject.GetComponent<AnotherScriptName>();
This is what I'd expect to do. Dig into the child of the object, and grab the component for that child.
However playing around with various ways of approaching this, it's not working.
Where am I slipping up, and what's the best way to set a value on a script that's under a child of an instanced gameobject?
Answer by tanoshimi · May 03, 2015 at 04:42 PM
Use the Find() method of the Transform component. http://docs.unity3d.com/ScriptReference/Transform.Find.html
Your answer
Follow this Question
Related Questions
Instantiating a new gameObject as a child of a different gameObject 2 Answers
Im having trouble making my character throw a gameobject 1 Answer
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
GetComponent vs AddComponent 3 Answers
Checking if object intersects? 1 Answer