Search Unity

Question gameObjects keep spawning even after I use .enabled = false; on spawner script

Discussion in 'Scripting' started by BloomyFractal, Aug 21, 2021.

  1. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    Hello, I want to stop my gameObjects from spawning as soon as my timer ends. The odd thing is, the Inspector tells me that the spawner script is not enabled, but the gameObjects keep spawning indefinitely after my timer reaches 00.00 seconds. Besides, I also made it so that the tray could not be moved anymore when the Timer ends, and it does work with the tray. I'd like to receive help to figure this out please. Unity gameObjects keep spawning despite the disabled script.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Pause the game and look for other instances of your spawner script. You can search for it by class name in the search bar of the hierarchy.

    Beyond that, what is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    I used Debug.Log(), but it's doesn't reveal a blatant issue for now. The log says that the spawner is set to false, but the prefabs still spawn. I don't know what I could do now. Unity BallSpawn Problem DebugLog prints.png
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Have you considered this option, which I listed above?

     
  5. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    I'm sorry, I don't understand how setting my script to some gameObject or the other would change a lot of things, since the only objects I destroy are the spheres, and I didn't attach any script on them. The goal of this Game Manager script is: As soon the Chronometer script's Timer value reaches 00.00 seconds, deactivate the tray control script and ball spawning script. Somehow it only works on the tray...?
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    put another way:

    - what if you inadvertently put TWO spawners in the scene
    - what if something ELSE is spawning stuff

    For instance, the thing you think is false... what happens if you DELETE it at runtime. Does it still spawn? If so, you can be pretty sure it's something ELSE spawning it!

    This is just logical debugging 101: destroy everything until you see the behavior go away, now you know who is responsible for that behavior. By destroying stuff while the game is running, you can trivially press STOP and all of it comes back.
     
  7. BloomyFractal

    BloomyFractal

    Joined:
    May 8, 2021
    Posts:
    16
    SOLUTION FOUND !!! I had attached my GameManager script to an object that is not the spawn manager; once I attached the GameManager to my spawnManager, removed it from the other, and wrote "Destroy(gameObject);" in my if statement, it worked ! Thank you for your help ! Unity correct Ball stop Script.png
     
    Kurt-Dekker likes this.