1

I am making a game in Unity2D i which you have to match the correct projectiles with the enemies but I can't randomly spawn them, it just spawns one... (btw this is my first game)

 { timeBTWSpawn = StartTimeBTWSpawn; private void Update() { if(timeBTWSpawn <= 0 ) { rand = Random.Range(0, enemies.Length); Instantiate(enemies[0], SpawnPoint.transform.position, Quaternion.identity);``` timeBTWSpawn = StartTimeBTWSpawn; } else { timeBTWSpawn -= Time.deltaTime; } } } i expect 3 different enemies to be randomly spawned but it only spawns the first one in the array. 
1
  • 2
    You should replace the enemies[0] with enemies[rand] because currently you are instantiating only the first enemy in the array, but if it was [rand] then you will be instantiating the enemy at the position returned by the Random.Range method Commented Oct 7, 2019 at 10:44

1 Answer 1

2

there is one minor mistake. You are not actually using "rand" variable inside the instantiate method. having 0 in the index will always spawn the first element inside enemies array.it should be like this:

Code:

 Instantiate(enemies[rand], SpawnPoint.transform.position, Quaternion.identity); 

this will fix :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot !! I was annoyed by this for so long and I had no idea the problem was this small

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.