Andere
Instantiating prefab causes existing instances to change values
I want to instantiate obstacles so I have a controller to instantiate from a prefab over time, that is assigned through a variable. I instantiate the prefab like so (Instantiation over time works fine)
Transform t = spawnX; // Transform to hold the x position of the spawn point
Vector3 v3 = t.position;
v3.y = Random.Range(minHeight, maxHeight); // Random y position
t.position = v3; // Get the random spawn point
// Instantiate the prefab
Instantiate(obstaclePrefab, v3, Quaternion.identity, t);
Again everything is working fine, except that when one instance is created, all already existing instances update their y-Position which was randomly created to the one of the new instance. Why is that?
Answer by UnityCoach · Feb 20, 2017 at 07:46 PM
You seem to parent the instance to the object transform "t" with your Instantiate call, so changing t will affect every children.
Follow this Question
Related Questions
How can i define a costume function for an object in a prefub that I spawn? 1 Answer
Unity gets hang and nothing work in it, even I have to close it from the task manager 0 Answers
How to spawn a prefab every 5 second randomly between a set range? 0 Answers
Want to instantiate Prefab in its parent. 1 Answer
Spawning objects at runtime in vertical-scroller game 0 Answers