Get components of multiple instantiated objects?

Hi, I have a script where I randomly spawn a given array of objects at a random time between 2 values. What I have been stuck on for the past hour is trying to get the transform component of the object I just instantiated, so I can reset the position after a certain time. Thus, making a pool for all my objects.

The code is as follows:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BugSpawn : MonoBehaviour

		public GameObject[] objlist;
		public float spawnmin = 1f;
		public float spawnmax = 2f;

		public bool firsttime;
		void Start ()
				firsttime = true;

				Spawn ();

		void Spawn ()
				if (firsttime == false) {
						Instantiate (objlist [Random.Range (0, objlist.GetLength (0))], transform.position, Quaternion.identity);

				Invoke ("Spawn", Random.Range (spawnmin, spawnmax));

				firsttime = false;

Thanks! any help is greatly appreciated :slight_smile:

“Instantiate” returns a reference to the object created. You can cast that reference to GameObject and then get the Transform component like you’ll do with any other GameObject.

if (firsttime == false) {
    GameObject instance = (GameObject)Instantiate (objlist [Random.Range (0, objlist.GetLength (0))], transform.position, Quaternion.identity);
    // do something with instance.transform

There’s a different method for instantiating objects and then accessing them later. It looks something like this

GameObject nameOfNewGameObject = Instantiate (objlist[Random.Range(0,objlist.GetLength(0))],tranform.position,Quaternion.identity) as GameObject;

nameOfNewGameObject.transform.position = new Vector3 (1,2,3);