1
\$\begingroup\$

I have different enemy AIs and two spawn-points in a scene, so far. The problem I am having is that only one enemy AI spawns, and the other enemy AI won't spawn, in that scene. How can I fix this problem?

Here is my code:

using UnityEngine;
using System.Collections;

public class EnemiesSpawner : MonoBehaviour 
{
    public GameObject [] enemy;
    public Transform [] spawnPoints;
    public float spawnTime = 5f;
    public Vector3 spawnValues;

    void Start () 
    {
        //Call the "Spawn" function every 10 seconds.
        InvokeRepeating("Spawn", spawnTime, spawnTime);
    }

    void Spawn () 
    {
        int spawnPointIndex = Random.Range (0, spawnPoints.Length); 

        Instantiate(enemy[Random.Range(0, enemy.Length)],
            spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

        Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), 
            1, Random.Range (-spawnValues.z, -spawnValues.z) );
    }
}
\$\endgroup\$
1
\$\begingroup\$

It's because your have single enemy assigned to instantiate.

You need an array of enemies to be spawned.

public GameObject[] enemy;

Then when you instantiate, either do it at random or as you need.

Instantiate(enemy[Random.Range(0, enemy.Length)], //will choose from enemy array
            spawnPoints[spawnPointIndex].position,
            spawnPoints[spawnPointIndex].rotation);

In usual case there are two random things to be taken care of. Random enemy and random point to spawn.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ – user1430 Aug 20 '16 at 14:49
  • \$\begingroup\$ I updated the question. \$\endgroup\$ – Freddy Aug 20 '16 at 14:50
  • \$\begingroup\$ Check chat @Freddy \$\endgroup\$ – Sourav Paul Aug 20 '16 at 14:55
  • \$\begingroup\$ What do you mean ? \$\endgroup\$ – Freddy Aug 20 '16 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.