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.

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;
      }