- Home /
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 :)
Answer by DiegoSLTS · May 05, 2015 at 02:24 AM
"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
}
http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
Hi there! firstly, thanks for the reply! I'm sorry that i hadn't mentioned this before but I already tried that and for some reason it doesn't recognise instance as a variable. I can't seem to put my finger on what's wrong.
Can you post the errors you're getting? There's a few other ways, like making a list/array. I'm interested to see what's wrong with your code at this point, though.
you probably have this:
if (firsttime == false) {
GameObject instance = (GameObject) Instantiate (prefab);
}
instance.transform.position = new Vector3();
your problem is that instance is local to the if statement and does not exist outside of it. You either need to perform the action within the if as shown in the answer or make the instance variable more global by declaring it outside the if statement.
Hi again! sorry for the late reply, I've been really busy with school. For some reason the code above started to work. Thanks for all your help guys! And thanks fafase for the global tip.
Answer by Addyarb · May 05, 2015 at 02:26 AM
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);
it is not different, it is just the cast that is different
var instance = (Type)action();
if action returns an object that cannot be cast to Type, the compiler crashes.
var instance = action() as Type;
if action returns an object that cannot be cast to Type, instance is null but no crash.
This is just a matter of do you need the object to cast or do you just want to try to cast.