- Home /
Parenting GameObjects
I have a script that spawns a bunch of game objects (in this case asteroids.) I have tried to create a master "spawn point" game object that acts as the parent for all the asteroids - this way I should be able to just move the spawn point without having to move each asteroid. Here is some psuedo code explaining this:
void Start(){
public GameObject spawnPointObject = (GameObject)Instantiate(some prefab, some vector, some quaternion);
for (int i = 0; i < 10; i++)
{
Vector3 localSpawnPoint = GetRandomPointAroundMe();
GameObject nextAsteroid = (GameObject)Instantiate(asteroidPreFab, localSpawnPoint, some quaternion);
nextAsteroid.transform.parent = spawnPointObject.transform;
}
}
My problem:
If I do not include the last line "nextAsteroid.transform.parent = spawnPointObject.transform;" all the objects appear around me as they should. If I DO include that line, they all appear on top of eachother - rather that at the vector3's I instantiated them at.
Any ideas?
Edit for additional info:
The spawning script is attached to a sphere that is a child of Camera.main (located at about 0,0,0)
private Vector3 GetRandomPointAroundMe()
{
float randomDistance = Random.Range(5,15);
Vector3 pointToReturn = Random.onUnitSphere * randomDistance;
return pointToReturn;
}
what is 'spawnPointObject' object is? what is it's scale?
Answer by Akill · Jul 31, 2012 at 06:57 PM
What object is this script attached? Can you show your GetRandomPointAroundMe() function?
If I were to guess, I would tell you to transform your localSpawnPoint into world coordinates, because more than likely, when you parent an asteroid, it will take a local position around that object instead of the world position you would like.
Thanks for the reply. I have edited my response to include the info you requested.
I am not too familiar with local position vs world position.
Your answer
Follow this Question
Related Questions
Instantiate a Prefab as child 0 Answers
parenting instantiated object 2 Answers
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer
Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer
Prefab, procedural attachment of child, childCount return 0 1 Answer