- Home /
How can i create/instatiating GameObjects of ThirdPersonController by script in c# ?
I want when running the game to have like 20 ThirdPersonControllers walking. Today what i did in the Hierarchy i dragged two ThirdPersonControllers and when running the game they are walking.
But i don't want to drag now another 18 of this in the Hierarchy. How can i make it with a script ?
What i tried so far is in the Hierarchy creating new empty GameObject and in a new c# script in the Start function i did a loop and then draged this script to the empty gameobject but when running the gam my whole pc frozed and memory was on 100% i had to reset my pc. This is the script:
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);
}
}
I can't reproduct the problem with the given script, make sure the problem comes from this script.
Also, make sure the numberOfObjects hasn't a value too high
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Why the ThirdPersonController is floating in the air when using Nav Mesh Agent ? 0 Answers
Renderer on object disabled after level reload 1 Answer
Third Person Flight? 0 Answers