Search Unity

Question (noob) How to completely destroy Enemy?

Discussion in 'Scripting' started by xpire1, Oct 19, 2020.

  1. xpire1

    xpire1

    Joined:
    Oct 19, 2020
    Posts:
    7
    My problem is that after I destroy enemy, then I switch the scene, when I go back to the previous scene enemy respawn, what should I do?
     
    Last edited: Oct 19, 2020
  2. Sphinks

    Sphinks

    Joined:
    Apr 6, 2019
    Posts:
    267
    You can put a flag to your enemy "isDestroyed" and check that flag if your scene will be loaded. If the flag is true you can destroy it again.

    Bu i don´t know, if that is the best way, maybe someone else knows something better.
     
    xpire1 likes this.
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Maybe you should not be using scenes for these transitions then? You can store your enemies in a static script so that when changes occur to them they will remain the same even after scene changes.
     
    xpire1 likes this.
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    That's kinda the point of scenes. They start in their default state.
    Any additional information needs to be provided by you. As others suggested, you could save changes to the scene (like an enemy being killed) in some arbitrary way, and then re-apply these same changes when you later load the scene again. Or you just dont change the scene - which ever is easier for your situation.

    I assume you have something like a persistant world and a second scene which may be, for example, inside some building. For a case like this you'd want to remember the world state and re-apply it when loading into the world again. That's similar to how you'd save the world state between game sessions.
     
    xpire1 likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I would switch all your enemies to prefabs, and just place empty GameObjects at their positions in the scene to be used as spawn points. When you leave the scene, record any state of the enemies in the scene you want to save for next time the scene is loaded. When you return to the scene you restore the enemies based on the previously saved state instead of using the spawn points.
     
    xpire1 likes this.