Instantiate prefabs at different position
hi guys. i have an array containing 3 different prefabs, and each prefab have its own position. Right now i could only instantiate them at one position. I want to know is there anyway i could set them at a specific position? like [o] at new vector3(0,0,0) [1] at new vector3(1,1,1) Thanks in advance!
ps. this is my code. public GameObject[] drops; for (int i = 0; i < drops.Length; i ++) {
Debug.Log(drops[i].ToString());
Instantiate(drops[i],transform.position, transform.rotation);
yield return new WaitForSeconds(waitTime);
}
in the instantiate method, you can specify the position of the objec. Ifit don't work, change the transform.position of the returned gameobject by the instantiate method : Gameobject go = Instantiate(prefab, position, rotation); go.transform.position = position;
Answer by LynoHD · Nov 03, 2015 at 10:58 AM
GameObject drop Instantiate(drops[i],transform.position, transform.rotation) as GameObject;
drop[0].transform.position = new vector3(1,1,1);
I THINK this should work
hi thanks for the reply! I've tried your code but it didnt work it just appear on the gameobject(the empty object i placed my script in) position ins$$anonymous$$d of vector3 1,1,1.
Answer by Garazbolg · Nov 03, 2015 at 09:32 AM
When instantiating you can specify the position with the second argument of the function :
Instantiate(yourPrefab, thePosition, theRotation);
You're actually already doing it but you're probably not moving the GameObject which has this script (transform.position refer to the position of that GameObject).
See for more informations : Instantiate Documentation
thanks for the reply! yea, but i want to set different position for different prefabs, but right now i can only set it all in one position