Need help with random enemies

Here is my code:

    public float SpawnRate = 6F;

    void Update()
      {
        if (VarRef.EndScene[VarRef.CurrPlayer] == false)
          {
            if (Time.time > NextSpawn)
              {
                if (EnemyCount < 10)
                  {
                    NextSpawn = Time.time + SpawnRate;
                    Spawn();
                  }
              }
          }
      }
    public void Spawn()
      {
        int i, j;
        int VehiclePass;
        int VehicleType = 0;
        VarRef.Load();
        if (VarRef.GameOver[VarRef.CurrPlayer] == false)
          {
            RandY = Random.Range(1, 4);
            WhereToSpawn = new Vector3(5.5F, VPos[RandY], 1F);
            if (VarRef.Level[VarRef.CurrPlayer] == 1)
              {
                EnemyNumber = Random.Range(1, 3);
                Instantiate(Enemy[EnemyNumber], WhereToSpawn, Quaternion.identity);
                VehiclePass = Random.Range(1, 5000);
                if (VehiclePass >= 3900)
                  {
                    VarRef.Vehicles[VarRef.CurrPlayer] ++;
                    if (VarRef.Vehicles[VarRef.CurrPlayer] > 3) VarRef.Vehicles[VarRef.CurrPlayer] = 3;
                    if (VarRef.Vehicles[VarRef.CurrPlayer] < 4)
                      {
                        aa:
                        VehicleType = Random.Range(1, 4);
                        if (VehicleType == 1)
                          {
                            //Cop Bike
                            j = 0;
                            for (i = 1; i <= 3; i++)
                              {
                                if (VarRef.VehicleList[VarRef.CurrPlayer, i] == 1) j = 1;
                              }
                            if (j != 0) {VehicleType = 0; goto aa;}
                            VarRef.VehicleList[VarRef.CurrPlayer, VarRef.Vehicles[VarRef.CurrPlayer]] = 1;
                            Instantiate(Vehicle_CopBike, new Vector3(Vehicle_CopBike.transform.position.x, Vehicle_CopBike.transform.position.y, Vehicle_CopBike.transform.position.z), Quaternion.identity);
                            if (VarRef.SoundActive[VarRef.CurrPlayer] == true) GetComponent<AudioSource>().PlayOneShot(Siren, VarRef.Sound[VarRef.CurrPlayer]);
                          }
                        if (VehicleType == 2)
                          {
                            //Cop Car
                            j = 0;
                            for (i = 1; i <= 3; i++)
                              {
                                if (VarRef.VehicleList[VarRef.CurrPlayer, i] == 2) j = 1;
                              }
                            if (j != 0) {VehicleType = 0; goto aa;}
                            VarRef.VehicleList[VarRef.CurrPlayer, VarRef.Vehicles[VarRef.CurrPlayer]] = 2;
                            Instantiate(Vehicle_CopCar, new Vector3(Vehicle_CopCar.transform.position.x, Vehicle_CopCar.transform.position.y, Vehicle_CopCar.transform.position.z), Quaternion.identity);
                            if (VarRef.SoundActive[VarRef.CurrPlayer] == true) GetComponent<AudioSource>().PlayOneShot(Siren, VarRef.Sound[VarRef.CurrPlayer]);
                          }
                        if (VehicleType == 3)
                          {
                            //Cop Helicopter
                            j = 0;
                            for (i = 1; i <= 3; i++)
                              {
                                if (VarRef.VehicleList[VarRef.CurrPlayer, i] == 3) j = 1;
                              }
                            if (j != 0) {VehicleType = 0; goto aa;}
                            VarRef.VehicleList[VarRef.CurrPlayer, VarRef.Vehicles[VarRef.CurrPlayer]] = 1;
                            Instantiate(Vehicle_CopCopter, new Vector3(Vehicle_CopCopter.transform.position.x, Vehicle_CopCopter.transform.position.y, Vehicle_CopCopter.transform.position.z), Quaternion.identity);
                            if (VarRef.SoundActive[VarRef.CurrPlayer] == true) GetComponent<AudioSource>().PlayOneShot(Copter, VarRef.Sound[VarRef.CurrPlayer]);
                          }
                      }
                  }
              }
...

I’m trying to generate random enemies. First, I’m getting many enemies overlapping each other. There’s only 3 lines for enemies to appear on, so I’m guessing I need to generate them less often; I’ve increased the value of SpawnRate, but that didn’t seem to help.

I’m also getting multiple of the same vehicle. Theoretically, it shouldn’t be able to have more than 1 of each at any given time (veh#1, #2 and #3 x1 each = 3 total). However, I’m getting two or three at a time, and overlapping each other.

Can anyone help me fix this mess?

Not sure, but one idea i have for the overlapping is you to try something like “public List createdEnemies;”. To track the enemy’s position. So after you have instantiated a new enemy add it to the list, “createdEnemies.Add (Vehicle_CopBike.transform.position);” . And before you are trying to “Instantiate” a new enemy check if there is already enemy in that place. “!createdEnemies.Contains(transform.position)”. So if the current place is empty then you can instantiate a new enemy there.

Sounds like a good idea. Could you give me an example?

Here’s what I have at the moment:

    void Start ()
      {
        VarRef = gameObject.AddComponent<Variables>();
        // Call the Spawn function after a delay of the spawnTime and then continue to call after the same amount of time.
        InvokeRepeating("Spawn", SpawnTime, SpawnTime);
      }
    public void Spawn()
      {
        ...
        Instantiate(Enemy[EnemyNumber], EnemySpawn[SpawnPointIndex].position, EnemySpawn[SpawnPointIndex].rotation);
        VehiclePass = Random.Range(1, 5000);
        if (VehiclePass >= 4900)
          {
            VarRef.Vehicles[VarRef.CurrPlayer] ++;
            if (VarRef.Vehicles[VarRef.CurrPlayer] > 3) goto ag;
            if (VarRef.Vehicles[VarRef.CurrPlayer] < 4)
              {
                aa:
                VehicleType = Random.Range(1, 4);
                if (VehicleType == 4) goto aa;
                j = 0;
                for (i = 1; i <= 3; i++)
                  {
                    if (VehicleType == 1 && VarRef.VehicleList[VarRef.CurrPlayer, i] == 1) j = 1;
                    if (VehicleType == 2 && VarRef.VehicleList[VarRef.CurrPlayer, i] == 2) j = 1;
                    if (VehicleType == 3 && VarRef.VehicleList[VarRef.CurrPlayer, i] == 3) j = 1;
                  }
                if (j != 0) {VehicleType = 0; goto aa;}

I’m still getting lots of overlap; I’m getting 5 and 6 enemies on exactly the same spot. I really, REALLY need help with this!

Hi. Don’t know if you have already found how to do this.
If not then as an example i have this random level generator script:
-More info below-

public GameObject[] tiles;
public int tileAmount;
public float tileSize = 1;

public float changeUp, changeRight, changeDown;

public float waitTime;

public List<Vector3> createdTiles;

void Start () {
        StartCoroutine (GenerateLevel());
    }

    IEnumerator GenerateLevel () {

        for (int i = 0; i < tileAmount; i++) {

// Since i am using 0 - 1 floats on "CallMoveGen()" then this needs to be also 0-1.
            float direction = Random.Range (0f, 1f);   
           
// randomly picking a tile to spawn, but can be used for something like this,
// "int tile = tiles[i];" so the tile changes.
            int tile = Random.Range (0, tiles.Length);

// This is for spawning the tiles
            CreateTile (tile);
           
//Moving the tile spawner
            CallMoveGen (direction);

// How fast do the tiles spawn. '0' is instantly
            yield return new WaitForSeconds (waitTime);

// If all the tiles are used then call "Finish();" where i instantiate walls and such.
            if (i == tileAmount - 1) {
                Finish ();
            }
        }
        yield return 0;
    }

// changeUp = 0.25f, changeRight = 0.5f, changeDown = 0.75, else = 0.75f-1.0f
    void CallMoveGen(float ranDir) {
        if (ranDir < changeUp) {
            MoveGen (0);
        } else if (ranDir < changeRight) {
            MoveGen (1);
        } else if (ranDir < changeDown) {
            MoveGen (2);
        } else {
            MoveGen (3);
        }
    }

    // Moving the spawner
    void MoveGen (int dir) {
        switch (dir) {
        case 0:
            transform.position = new Vector3 (transform.position.x, transform.position.y + tileSize, 0);
            break;
        case 1:
            transform.position = new Vector3 (transform.position.x + tileSize, transform.position.y, 0);
            break;
        case 2:
            transform.position = new Vector3 (transform.position.x, transform.position.y - tileSize, 0);
            break;
        case 3:
            transform.position = new Vector3 (transform.position.x - tileSize, transform.position.y, 0);
            break;
        }
    }
//Now creating the tile
    void CreateTile (int tileIndex) {

//Checking if there is a free empty tileSized space to spawn. Otherwise try the spawn again.
        if (!createdTiles.Contains (transform.position)) {
       
// Cloning the object because of optimizing thingies
            GameObject tileObject;

// Instantiate the cloned object
            tileObject = Instantiate (tiles [tileIndex], transform.position, transform.rotation) as GameObject;

// Adding the cloned objects position into the list of Vector3s
            createdTiles.Add (tileObject.transform.position);

        } else {
            tileAmount++;
        }
    }

In this script i am basically running the “GenerateLevel()” coroutine loop when the game starts until all the tiles are used. And in every loop i am creating random numbers for direction and what tile to use. But if you already have spawn locations where you spawn your enemies then you can use those so there is no need to use the randomly created spawn positions like i did.
Then i am creating the tile “CreateTile()” which checks if there is an empty tile position so the tile can be spawned. Otherwise try again later after the spawn location has changed via “CallMoveGen()” which checks if the random numbers are bigger or smaller as already given values and move the spawner “MoveGen()” void according to the numbers.
I don’t think this script will work for you as is but i hope it will give a some sort of idea.

Thanks. I already got a lot of help from another user, and completely resolved this particular problem.