- Home /
Change Parent of Multiple Instantiated Objects
Hello,
I'm currently working on a script which instantiates multiple instances of a prefab under a pre-existing parent. I've been through the forum and found out how to change the parent with a script, but it only does the first instance. I then tried populating an array by finding tagged objects and acting on that, but i must be missing something.
Here's what i have so far,
var shot : Transform;
var StringParent : Transform;
var recoveryTime = 10;
var numberOfCaps : int = 10;
private var kids :GameObject[];
private var delay = 0;
function Update () {
if (delay>0){delay -=1;}
if (delay==0){
if(Input.touchCount == 1){
var currentTouch:Touch = Input.touches[0];
if(currentTouch.phase == TouchPhase.Began && guiTexture.HitTest(currentTouch.position)){
print ("Spawn");
for(var i : int = 0; i < numberOfCaps; i++)
var shot = Instantiate(shot, Vector3 (Random.Range(0.5, 1) , Random.Range(10, 110) , Random.Range(0.5, 1) ), Quaternion.identity);
delay = recoveryTime;
var kids = GameObject.FindGameObjectsWithTag("spawned");
for (var spawned in kids)
{
transform.parent = StringParent;
}
}
}
}
}
Love some tips,
Thanks.
Answer by syclamoth · Oct 25, 2011 at 01:41 AM
One-line fix- in your for loop, you need to reference the 'spawned' inside of kids every loop! Like this-
for (var spawned in kids)
{
spawned.transform.parent = StringParent;
}
I was sure i did that!! i guess not though, because it didn't work before, and now it does. Thanks heaps, this is the best forum i've ever used, so many people with so much knowledge being so generous with their time!
You Rule!
Your answer
Follow this Question
Related Questions
Updating a variable on a script in an instanced object 1 Answer
Instantiating prefab with certain coordinates problem (c#) 0 Answers
Prefab, procedural attachment of child, childCount return 0 1 Answer
How to instantiate from prefab, not from instance? 3 Answers
How to get all objects instantiated from a single prefab? 1 Answer