- Home /
why does "new GameObject" creates 2 emptys
so i got this for loop to spawn a series of empty objects at a certain distance from each otehr, its the first time i use "new GameObject" so im not sure if its supposed to spawn two game objects or if im doing something wrong. here is the code:
public GameObject sun;
//public Vector3[] sunPosXAxis;
//public Vector3[] sunPosYAxis;
public int planetAmount;
public float planetSpacing;
public GameObject[] planetSpawn;
public Vector3[] planetsPos;
// Use this for initialization
void Start () {
Instantiate(new GameObject("Sun"), Vector3.zero, Quaternion.identity);
SpawnPlanets();
}
void SpawnPlanets () {
for (int i = 0; i < planetAmount; i++){
GameObject clone = Instantiate(new GameObject("Planet_Spawn" + i), new Vector3(planetSpacing * i, 0, 0), Quaternion.identity) as GameObject;
planetsPos[i] = clone.transform.position;
}
}
if someone knows why it does this or how to stop it pls tell me, thank you all
Answer by devluz · Nov 05, 2014 at 10:10 PM
You spawn a new one with new GameObject already. You don't need Instantiate. Using this method will spawn another one based on the one you create with new.
GameObject go = new GameObject("Planet_Spawn" + i));
go.transform.position = new Vector3(planetSpacing * i, 0, 0);
is all you need.
Your answer
Follow this Question
Related Questions
list.contains isn`t working 1 Answer
My grid is getting created on the wonk 1 Answer
How to correctly shorten this script using arrays and iterations 2 Answers
change material in children 3 Answers
Problem with for... in loop 1 Answer