Search Unity

Survival Shooter enemy spawn problem

Discussion in 'Getting Started' started by Wolfgabe, Sep 13, 2016.

  1. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
  3. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyManager : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;
    6.     public GameObject enemy;
    7.     public float spawnTime = 3f;
    8.     public Transform[] spawnPoints;
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    14.     }
    15.  
    16.  
    17.     void Spawn ()
    18.     {
    19.         if(playerHealth.currentHealth <= 0f)
    20.         {
    21.             return;
    22.         }
    23.  
    24.  
    25.  
    26.         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    27.  
    28.         Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    29.  
    30.         Debug.Log;
    31.     }
    32. }
    33.  
    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);
     
    Last edited: Sep 13, 2016
  4. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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
    upload_2016-9-12_22-44-46.png

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

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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).
     
  6. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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)
     
    Last edited: Sep 13, 2016
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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?
     
  8. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    I was able to fix that error. The enemy manager is the same script attached three times
     
  9. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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:

    Code (CSharp):
    1. var noob =  Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    2. 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!).
     
  10. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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
     
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
  12. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyManager : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;
    6.     public GameObject enemy;
    7.     public float spawnTime = 3f;
    8.     public Transform[] spawnPoints;
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    14.     }
    15.  
    16.  
    17.     void Spawn ()
    18.  
    19.  
    20.     {
    21.  
    22.  
    23.         if(playerHealth.currentHealth <= 0f)
    24.  
    25.         {
    26.             return;
    27.         }
    28.          
    29.         int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    30.  
    31.  
    32.          var noob = Instantiate (enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    33.          Debug.Log("Instantiated " + noob + " at " + spawnPoints[spawnPointIndex].position, noob);
    34.  
    35.  
    36.     }
    37. }
    38.  
    39.  
    40.  
    my current enemy manager code
     
  13. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
  14. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    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
     
  15. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    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.
     
  16. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    tedthebug likes this.
  17. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    I pretty much have managed to finish the tutorial the only thing that is off is the lighting
     
  18. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    So what was the issue?
     
  19. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    I think it was something with the scripts being attached to the enemy controller I am not sure
     
  20. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    "It doesn't work and I have no idea why"
    "It works and I have no idea why"
    Every programmer can relate.
     
  21. Wolfgabe

    Wolfgabe

    Joined:
    Sep 4, 2016
    Posts:
    131
    guess I am not the only one then. I am mostly a complete programming noob and its mainly been through Unity I have been learning scripting
     
  22. Deleted User

    Deleted User

    Guest

    I had exactly this problem and in 2 hours found the answer (thanks to Debug.Log()). I had drugged in the field Player Health not object Player from Hierarchy but prefab Player from Project Window. The prefab Player had 0 health and Enemy Manager was returning from function Spawn().