- Home /
Spawning an array
I have a ball, and when it hits the spikes I'm trying to cause an effect of him breaking down, all his parts are falling, I need to instantiate 5 prefabs I've made at the same time at the same position.
I tried this but give me errors, how can i instantiate an array of gameobjects?
public GameObject[] obj;
foreach (GameObject i in obj)
{
Instantiate(i, transform.position,Quaternion.identity);
}
I'm not sure, but I would try GameObject temp = Instantiate(etc.); ins$$anonymous$$d of only Instantiate.
Answer by DoTA_KAMIKADzE · Apr 26, 2015 at 01:27 PM
Code looks fine. So far that I think there might be 2 errors:
1)You haven't set your objects in your obj array then it should give you null exception. If so then provide them in Inspector or through code...
2)You're running that code from script that doesn't inherit MonoBehaviour then you shouldn't be able to compile with something like "transform doesn't exists" or something like that. If so then either provide transform to that script or use Vector3 as a position.
And remember to post the actual error next time...