- Home /
instantiate multiple gameobject of the same gameobject 2d
how to instantiate multiple game object of the same game object with a different direction in a 2d i had mad an enemy i want him when he dying spawn for an example 10 coin in a different direction thanks @numberkruncher @bigdaddy @Bioshok2 @codezeero @LuckierBread @BerggreenDK
Answer by highpockets · Apr 04, 2019 at 07:27 AM
You can use instantiate inside a for loop that cycles through the loop 10 times and uses random range to dynamically place the coins:
int coinCount = 10;
for( i = 0; i < coinCount; i++){
Instantiate(prefab, new Vector3(enemy..transform.position.x + Random.Range(-10.0f, 10.0f),enemy..transform.position.y + Random.Range(-10.0f, 10.0f), 0), Quaternion.identity);
}
if you want to have the coins facing different directions, you can rotate each one with Random.Range as well, but I believe you just wanted 10 coins to appear in random positions close to the enemy when he dies, but be sure to either save the position of the enemy before you destroy him or destroy him after the for loop runs because the script above assumes that the enemy game object is still active