Chapter 5 Aliens Won't Spawn and Variable Problem

HI, my aliens won’t spawn,
Here is my GameManager.cs script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Collections.Generic;

public class GameManager : MonoBehaviour {
public GameObject player;
public GameObject[] spawnPoints;
public GameObject alien;

public int maxAliensOnScreen;
public int totalAliens;
public float minSpawnTime;
public float maxSpawnTime;
public int aliensPerSpawn;

private int aliensOnScreen = 0;
private float generatedSpawnTime = 0;
private float currentSpawnTime = 0;

// Use this for initialization
void Start () {
	
}

// Update is called once per frame
void Update () {
    currentSpawnTime += Time.deltaTime;
    if (currentSpawnTime > generatedSpawnTime)  {
        currentSpawnTime = 0;
        generatedSpawnTime = Random.Range(minSpawnTime, maxSpawnTime);
        if (aliensPerSpawn > 0 && aliensOnScreen < totalAliens) {
            List<int> previousSpawnLocations = new List<int>();
            if (aliensPerSpawn > spawnPoints.Length)  {
                aliensPerSpawn = spawnPoints.Length - 1;
                aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn
                - totalAliens : aliensPerSpawn;

                for (int i = 0; i < aliensPerSpawn; i++)  {
                    if (aliensOnScreen < maxAliensOnScreen)   {
                        aliensOnScreen += 1;
                        // code goes here

                        // 1
                        int spawnPoint = -1;
                        // 2 
                        while (spawnPoint == -1) {
                            // 3
                            int randomNumber = Random.Range(0, spawnPoints.Length - 1);
                            // 4
                            if (!previousSpawnLocations.Contains(randomNumber)) {
                                previousSpawnLocations.Add(randomNumber);
                                spawnPoint = randomNumber;
                            }
                        }
                        GameObject spawnLocation = spawnPoints[spawnPoint];
                        GameObject newAlien = Instantiate(alien) as GameObject;
                        newAlien.transform.position = spawnLocation.transform.position;
                        Alien alienScript = newAlien.GetComponent<Alien>();
                        alienScript.target = player.transform;
                        Vector3 targetRotation = new
                            Vector3(player.transform.position.x,
                            newAlien.transform.position.y, player.transform.position.z);
                        newAlien.transform.LookAt(targetRotation);
   
                    }
                }
            }
        }
    }
}

}

And here is my Alien.cs Script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.AI;

public class Alien : MonoBehaviour {

public Transform target;
private NavMeshAgent agent;

// Use this for initialization
void Start () {
    agent = GetComponent<NavMeshAgent>();
}

// Update is called once per frame
void Update () {
    agent.destination = target.position;
}

}

Please tell me how I can fix my code in order to spawn aliens.
Thank you.

@vegetarianzombie Can you please help with this when you get a chance? Thank you - much appreciated! :]

@vsummer7 I had the same issue for a bit. The issue is that:

if (aliensPerSpawn > spawnPoints.Length)  {
            aliensPerSpawn = spawnPoints.Length - 1;
            aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn
            - totalAliens : aliensPerSpawn;

Should be

if (aliensPerSpawn > spawnPoints.Length)  {
                aliensPerSpawn = spawnPoints.Length - 1;
}

So the if statement execution branch is taking in WAY too much.

1 Like

Hi!

I’m reading this fragment now. Should not there be just aliensPerSpawn = spawnPoints.Length? Why minus one? As I see it, aliensPerSpawn should be exactly from 1 to maximum amount that is spawnPoints.Length, but not spawnPoints.Length - 1. We should be able to spawn 1-11 enemies, but not 1…10. There is even a comment just after this piece of code:

This limits the number of aliens you can spawn by the number of spawn points.

But this code limits it by -1 extra.

Besides the subsequent loop “for” goes from 0 to aliensPerSpawn with “less” condition <, not <=.

When if-block (or similar code block like loop/case) contains only one line of code for its body, I prefer not to use brackets at all. I think it’s much more clear (especially considering code formatting and indentation) and prevents from accidentally adding extra code to the block body like in this case. So it looks like this:

if (aliensPerSpawn > spawnPoints.Length)
    aliensPerSpawn = spawnPoints.Length - 1;
aliensPerSpawn = (aliensPerSpawn > totalAliens) ? aliensPerSpawn - totalAliens : aliensPerSpawn;

There are some special cases when you must use brackets even for one-line bodies like in ASP.NET Razor cshtml views, but in this case it is just a regular C# code.

That’s a matter of personal preference. Readability is subjective, I personally like all my if-statements to have brackets, that also makes it easier to add logic to it later on. It’s also less confusing for beginners since there’s the risk they’ll add another line below thinking it’ll be part of the if-statement.

Of course, when following along with a tutorial or a chapter (or writing your own code), you can choose to omit the brackets for one liners if you wish. With that said, most articles recommend to add the braces anyway to keep everything consistent.

Yes, I agree. But I had one more question before :slight_smile: Could you please answer it?

@vegetarianzombie might be able to help with that. I’m not familiar with the code.
Does the code work correctly if you leave the -1 out? There might be a reason later on why it’s like that.

Cheers

Oh, yeah, I see. I’ll check this code a bit later.