- Home /
Making multiple instances of a prefab,Instantiating multiple instances of a prefab
Hey, im making a 2d infinite runner and im having an issue with making multiple instances of one prefab. I have a script that is attached to the prefab and is run when the prefab is added to the scene, it changes positions of elements in prefab. When i make the first instance of the prefab it works fine, it randomizes positions of elements, but next instances of prefab do not randomize position of elements, they are copies of first instance instead of new instances based on prefab located in assetss. Im struggling to fix this problem, any help would be appreciated,Hello, im making 2d procedural generated infinite runner. I generate my platforms base on a prefab which has a script attached that relocates platforms inside this prefab when prefab is instanted. I create them in chunks 3 at a time, the problem is that the first one instance of prefab is having its platforms relocated but the second and the third have the same platforms locations, as if the third was copy of the second from scene, not a new Instance of prefab from assets. Im new to unity and struggling to resolve this issue. Help would be really appreciated.
Answer by rvilgalys · Nov 26, 2017 at 08:36 PM
Hard to say without knowing the code. Where are you generating the random information?
First guess would be that you're starting with the same Random.seed for each instance so they're picking the same "random" numbers.
@rvilgalys Ill explain my flow, i make instances of prefabs in a loop, every fuction call i make 3 instances. First one is ok, but the second and the third have the same position of elements inside. Below is the part of the script which relocates elements.
private void randomizePosition()
{
System.Random random = new System.Random();
foreach(Transform child in children.GetRange(0,2))
{
Vector3 newPos = new Vector3(random.Next((-tileWidth / 2), (-tileWidth / 4)), random.Next((-tileHeight/ 2), 0));
Vector3 newSCale = new Vector3(random.Next(1, 3),child.localScale.y, 0);
child.transform.localPosition = newPos;
child.transform.localScale = newSCale;
}
foreach (Transform child in children.GetRange(2,2))
{
Vector3 newPos = new Vector3(random.Next((0), (tileWidth / 2)), random.Next((-tileHeight / 2), 0));
Vector3 newSCale = new Vector3(random.Next(1, 3), child.localScale.y, 0);
child.transform.localPosition = newPos;
child.transform.localScale = newSCale;
}
}
It is weird because the function that makes 3 instances of prefab is called in update() function and every time new prefabs instances are made the first one is ok but the second and third are the same And yea, ive checked and the "random numbers" are the same but its impossible for this to be just a coincidence. It happens like 100 times in a row
Your answer
Follow this Question
Related Questions
Some components missing in the prefab (clone) 0 Answers
Instantiated Prefabs Are Destroyed After 4 instantiations 1 Answer
prefab navagent doesnt work,prefab navmesh doesnt work 1 Answer
how to replace multiple game objects with different transform component with same prefab? 1 Answer
Can't modify BoxCollider center/size on base prefab 0 Answers