- Home /
How to randomly generate prefab?
Is it possible to instantiate a prefab (say, a cylinder 100 times) each at various size and distance apart, all contained within a circle and not overlapping each other?
Essentially, what I'm trying to create is something like a a colour perception test.
Answer by gfvfubb · Jan 07, 2013 at 09:01 AM
http://answers.unity3d.com/questions/12003/instantiate-a-prefab-through-code-in-c.html
http://docs.unity3d.com/Documentation/ScriptReference/Resources.html
This last one is easier than instantiating a prefab though if all you need are a bunch of cylinders: http://docs.unity3d.com/Documentation/ScriptReference/GameObject.CreatePrimitive.html
Put that in a for loop, along with some coloring added with http://docs.unity3d.com/Documentation/ScriptReference/GameObject.AddComponent.html
And then use Math.f Sin / Cos with an interior argument multiplied by 1/100 to generate the position coordinates of a large circle.
Answer by Maulik2208 · Jan 07, 2013 at 08:54 AM
1) Have a function which gives a unique random number so issue of overlapping will be solved-----> Check here
2) Make a function to place objects dynamically (Use Random.range) For ex: Instantiate (yourprefab,Vector3(Random.Range (0,4),Random.Range (0,4),Random.Range (0,4)));// here 0 and 4 represent the range to have number between
3) Then have a third function to change the color or etc you want.....
Don't forget to mark the answer if found useful...Cheers