Survival Shooter enemy spawn problem

now I have been following the survival shooter tutorials. I have the enemy spawns setup and everything seems to work fine for the most part but when I test only the zombunnies spawn while the bears and hellephants dont appear at all. I am not sure what did wrong. Can anyone help me with this?

Put a Debug.Log in the code that spawns these other baddies. Then put a Debug.Log in whatever should be calling that code. Continue in this way, working backwards, until you pinpoint the problem.

Femember, your program is just a machine. These is no mystery to what it does, if you take the time to look carefully.

using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    public PlayerHealth playerHealth;
    public GameObject enemy;
    public float spawnTime = 3f;
    public Transform[] spawnPoints;


    void Start ()
    {
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }


    void Spawn ()
    {
        if(playerHealth.currentHealth <= 0f)
        {
            return;
        }



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

        Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);

        Debug.Log;
    }
}

like this?

Edit okay I tried putting Debug.Log (enemy) in the void spawn area and did get one message which was

ZomBear 1 (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
EnemyManager:Spawn() (at Assets/Scripts/Managers/EnemyManager.cs:22)

but it still doesnt explain my issue

then I tried putting in spawnpoints and still nothing

now I have tried

Debug.Log (spawnPoints[spawnPointIndex].position,spawnPoints[spawnPointIndex].rotation);

Edit the debug code isnt really getting me anywhere either even when it shows me a message it just leads me right back to where I typed the line in the first place. I really wanna figure out whats

2786042--201819--upload_2016-9-12_22-44-46.jpg

here is my Enemy Manager with the various spawnpoints set. I did exactly what the tutorial said.

Yes, a Debug.Log shows you that the code (where you put the Debug.Log) actually executed, and when. Now you need to compare that output to the expected output. For example, from what you showed above, I would expect that log message to appear every 3 seconds with a Zombear, every 3 seconds with a Zombunny, and every 10 seconds with a Hellephant. Is that what you see in the console or not?

Logging the spawn points is a reasonable idea, if you have reason to suspect that the new objects are being spawned in the wrong place. But that’s easy to tell anyway: just watch the Hierarchy window. If you see objects appearing there that you don’t see in the scene, then they are hidden somehow. If you don’t see the objects in the scene, then either (a) your code is not actually executing (the Debug.Logs can falsify that); (b) the objects are not being spawned; or (c) the objects are being spawned but immediately disappearing. There are no other possibilities, so you just have to figure out which of these is actually going on.

As a general note: I notice that your window layout doesn’t include the Console. I hope that’s because you have the Console filling your entire second monitor! You should always have the Console visible, and big enough to show at least a half-dozen messages. You should be watching it while your program runs, and seeing if the expected messages appear when they should.

P.S. I’d wager heavily that you did not do exactly what the tutorial said, because if you did, you’d get the results the tutorial shows. But claiming or denying this is not helpful either way. You have a program that doesn’t do what you want; so you have to figure out why, and fix it. This is 90% of what it means to be programming (at least, at the entry level).

The objects are not appearing and suddenly despawning as they dont show up in the hierarchy. I have mainly deduced the objects are not spawning and yes when I put the Debuf.Log in with the void spawn I did see zombear showing up in my error log every three seconds and hellephant every 10

ZomBear (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
EnemyManager:Spawn() (at Assets/Scripts/Managers/EnemyManager.cs:21)

Hellephant (UnityEngine.GameObject)
UnityEngine.Debug:Log(Object)
EnemyManager:Spawn() (at Assets/Scripts/Managers/EnemyManager.cs:21)

The screenshot of your hierarchy shows an error in the console. Have you fixed that error?

Also, do you have 3 different scripts with the same name on the object or have you attached the one script 3 times?

I was able to fix that error. The enemy manager is the same script attached three times

Hmm. So the code is executing with ZomBears and Hellephants, but you don’t see them in the hierarchy.

What happens if, while the game is running, you grab your ZomBear prefab (get to this by clicking “ZomBear” in the Enemy slot of your EnemyManager script, as shown in the screenshot above), and drag it into the Hierarchy? Do you get a new ZomBear that hangs around in the scene, or does it somehow disappear as soon as you let go?

Assuming it stays, then the Instantiate line ought to do the same thing. Let’s log a little more detail. Change the current Instantiate line to this:

var noob =  Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
Debug.Log("Instantiated " + noob + " at " + spawnPoints[spawnPointIndex].position, noob);

That should should you the name and position of the newly instantiated object, and also, when you click that message in the console, it should highlight the object in the Hierarchy.

Don’t worry, we’ll get to the bottom of this (and it will turn out to be something simple!).

Okay I drag the bears and elephants into the hierarchy and they stick around and behave like they are supposed to. I changed the instantiate line to what you said but it only shows the zombunny instantiations

Now I really am confused. Above, you said:

But now, you say it’s not doing this anymore? It’s only showing the zombunnies? Something must have changed besides just the more detailed logging.

what I meant was I put the zombears and hellepants in the hierarchy and they behave normally but with the debug log only the zombunnies are mentioned in the console when I put it like you showed me

using UnityEngine;

public class EnemyManager : MonoBehaviour
{
    public PlayerHealth playerHealth;
    public GameObject enemy;
    public float spawnTime = 3f;
    public Transform[] spawnPoints;


    void Start ()
    {
        InvokeRepeating ("Spawn", spawnTime, spawnTime);
    }


    void Spawn ()


    {


        if(playerHealth.currentHealth <= 0f)

        {
            return;
        }
          
        int spawnPointIndex = Random.Range (0, spawnPoints.Length);


         var noob = Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
         Debug.Log("Instantiated " + noob + " at " + spawnPoints[spawnPointIndex].position, noob);


    }
}

my current enemy manager code

Yes, I understand that’s what you meant in that post; I’m talking about the post several ones up, where you said:

This is really important. Let’s not gloss over it. At one point in this process, you had debug output showing ZomBear and Hellephant. Now you say you’re only seeing debug output for ZomBunnies. We need to figure out why – what changed?

Double-check that all three instances of your script are still enabled, that they’re hooked up to the right prefabs, that you have at least one spawn point set, etc.

I put a another debug.log in the void spawn and I went to my enemy manager deleted all my scripts and reattached them and everything now the bears and elephants spawn properly. still I am not sure why my lighting is off

I’m likely wrong since I’m nowhere near being a programmer, but I thought you could have the same script attached to multiple objects but you can’t have the same script attached multiple times to one object. In the screenshot above your EnemyManager object has the same script attached 3 times & it appears to only be auctioning the first one shown in the inspector for your EnemyManager.

No, you can attach the same script to an object multiple times. It only causes grief if you try to get a reference to them using GetComponent, since obviously, the type isn’t enough to identify which one you mean. But other than that, it works fine.

1 Like

I pretty much have managed to finish the tutorial the only thing that is off is the lighting

So what was the issue?

I think it was something with the scripts being attached to the enemy controller I am not sure

“It doesn’t work and I have no idea why”
“It works and I have no idea why”
Every programmer can relate.