- Home /
instantiating 10 objects at runtime.
am doing a space shooter game where i need to make enemies. i want to make instance of the enemies. should i have to use for loop for multiple instanciations. if so can any one tell how. i have never tried like this before. i need a start. need help.
Answer by gajdot · Nov 20, 2013 at 09:54 AM
Yes, you need a for loop or while. You could look at this documentation:
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Basically the first example is what you need, the only thing you could change to add an array of transform and use it's coordinates to spawn enemies like this:
using UnityEngine; using System.Collections;
public class Example : MonoBehaviour {
public Transform[] spawnLocations;
public gameObject prefab;
void Example() {
int i = 0;
while (i < 10) {
Instantiate(prefab, spawnLocations[i].position, spawnLocations[i].rotation)
i++;
}
}
}
And just set up the spawnLocations by adding the spawnlocations from the inspector panel.
Answer by ramp · Nov 20, 2013 at 11:39 AM
Hi,use this code in java script.instantiate 10 object random with space 2 from each other in game view. var prefab : Transform; function Example() { for (var i : int = 0;i < 10; i++) { Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity); } }
Your answer
