- Home /
Creating circles using script
I am very new to Unity. I have a very simple problem. I need to generate an arbitrary number of circles (sprites?) in my scene. How do I do this with scripting? For such a simple problem I cannot seem to find a clear solution.
Answer by zohaibzaidi · Nov 28, 2018 at 05:48 AM
If I understand correctly you just need a for loop.
make a public reference to the sprite you want to instantiate.
public GameObject spritePrefab;
void SpawnSprites()
{
for(int i = 0 ; i < numOfSprites ; i++)
{
GameObject go = Instantiate(spritePrefab);
//you can set position and whatever here
}
}
In the example you have written, Instantiate acts like a duplication of spritePrefab, correct? Does this mean I need to modify spritePrefab in some way to be a circular object/sprite? Or do I have to modify the go objects to achieve this.
*Edit Never$$anonymous$$d. If I'm not mistaken I can reference sprites in my assets?
Yes you can reference sprites.
Also what you can do is make prefabs. I suggest reading up on what a prefab is. You can then reference your prefab and instantiate it. The prefab does not have to be in the scene hierarchy. It can be referenced from any where under the Assets folder.
Your answer
Follow this Question
Related Questions
Question on Sprite and Movement 0 Answers
Rotate around a circle 0 Answers
My sprite from sprite.create looks pixelated. 1 Answer
Moving in circles 1 Answer
Handling Multiple Resolutions in 2D? 0 Answers