Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Game Objects with tag Enemy respawning when not supposed too.

Discussion in 'Scripting' started by jevanswow, Jun 13, 2015.

  1. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    I am working on my combat function and to test out the code I created a cube with a Enemy tag on it. The way I have it is when the currhealth on it reaches below 1 it is supposed to be destroyed. The destroy part works just fine however when I load up the menu or the character screen (both of them has problems) and click on return, the game object respawns.

    If anyone needs more of a code than this I'll be more than happy to supply more but as far as the code that controls it on the player attack it is, GameObject enemy = GameObject.FindGameObjectWithTag ("Enemy");
    DestroyObject (enemy);


    And the code for the menu and character screen is GameObject go = GameObject.FindGameObjectWithTag ("Player");

    I'm not sure if this would make any difference in the problem I'm having but I also got a

    if (huntscout.enemyscoutquest == false && enemyalive == true), in there as well and once the enemy is dead it would switch enemyalive to false.

    My apologies if there are any confusion and if anyone else had this problem before can you please let me know what you did to fix it?

    Also I have noticed that while I'm getting a lot better in Unity Coding and C# coding and I can do a lot more, it seems like I'm just putting in more logic errors than I intend to, for example in this case so if anyone knows what part of the Unity documentation or any of the video lessons they can point me to that would also be helpful.

    Thank you.
     
  2. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    I was just thinking of something, I'm not sure if this would make any difference but on the two scripts that loads the menu and character screen I have a Application.LoadLevel in there, and also a void OnLevelWasLoaded(), is it possible after the level (in the case of the character screen level 6) gets reloaded (level 1) that whether game objects that was in there including the destroyed one get reloaded? And if so any ideas on how to keep those destroyed game objects from being respawned.

    I apologize for the confusion and the poor terminology but as I said, while I can code a lot more, I still have a long way to go.
     
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    When you do an Application.LoadLevel everything in the current scene is destroyed. You can have objects persist across scenes with Object.DontDestroyOnLoad. But that isn't really applicable to your situation. You will need to save the state of all objects in your scene, and then recreate the scene once you return from your menu/character screen.
     
  4. jevanswow

    jevanswow

    Joined:
    Apr 17, 2015
    Posts:
    33
    Thanks for the help there. As far as the documentation goes, as big as the game I'm working on I probally should go ahead and take a day and read all of the documentation that way if I do happen to code something and it doesn't make sense logically at least I'll know how to fix it.