- Home /
Updating Pattern of Prefabs
In the Unity docs is an example of positioning prefabs in a pattern. For example a circle. My question is, what if you want to update the radius of the circle so that the prefabs expand from the center as you drag the mouse. I can set the point of initiation to where I click the mouse and get the distance from that position to current mouse position as I drag to set the radius. But how do I actually update the positions to the new radius? Thanks.
public GameObject prefab;
public int numberOfObjects = 20;
public float radius = 5f;
void Start() {
for (int i = 0; i < numberOfObjects; i++) {
float angle = i * Mathf.PI * 2 / numberOfObjects;
Vector3 pos = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)) * radius;
Instantiate(prefab, pos, Quaternion.identity);
}
}
Answer by Xtro · Aug 22, 2013 at 05:41 PM
You should store the prefab instances into a list.
In update event, you should loop the items in that list and update them 1 by 1.
What would the limitations of this approach be? I have a prefab that is a simple plane consisting of 2 triangles. No textures, just the most basic material. How many instances could I create and update? 100s, 1000s? Essentially what I'm asking is: could I in theory use the planes as "pixels"? To create patterns and geometric shapes from them? Thanks
I didn't test such a thing but 100s on mobile and 1000s on desktop should be a problem I guess.
if you see a performance drop, you can update them in odd/even rounds to double the performance.
in first update, just update odd numbers in the list, in next update, just update the even numbers.
Can you please mark the correct answer if you fixed that problem?
Your answer
![](https://koobas.hobune.stream/wayback/20220613112735im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Tutorial for manual instantiation of prefabs yields null exception 2 Answers
Using a Master Control Script w/Instantiated prefabs & multiple scenes? 0 Answers
How can I create a prefab variant in C# script? 1 Answer
Prefabs Workflow - Changing models in a 3D app after they have been used in a prefab 9 Answers
Independence for scripts in clones? 1 Answer