Search Unity

Cant DontDestroyOnLoad by Tag

Discussion in 'Scripting' started by daggasoft, Jul 24, 2013.

  1. daggasoft

    daggasoft

    Joined:
    Jun 29, 2013
    Posts:
    22
    Hey, first off I'm very very new to this.

    I'm trying to not destroy object on new level load by tag. In my game I have a lot of coins that get instantiated when a shell hits a block (think mario brothers). At the end of the level, on new level load, IF any of these coins are still on screen, they disappear. I want them to persist to the next level and die on their own (I have them commit suicide after 3 seconds). Reason being is I want the animation and sound to not be abruptly terminated upon new level.

    Using an example on how to Destroy any objects with a given tag, I figured I could just replace destroy with dontdestroyonload. But in practice that isn't working. Is this because I'm instantiating clones or something like that? Any suggestions on how to handle this?

    GameObject[] names = GameObjects.FindGameObjectsWithTag("coin");

    foreach(GameObject item in names)
    {
    DontDestroyOnLoad(item);
    }
     
  2. Deleted User

    Deleted User

    Guest

    That code looks alright to me. You should narrow down exactly which part is failing - whether FindGameObjectsWithTag is not returning the set of GameObjects you expect or if DontDestoryOnLoad isn't having the expected effect. If it's the former, I'd check that the tag name is exactly right and if the GameObjects are active when you're searching for them (if they're inactive, they can't be found with the Find functions)
     
  3. daggasoft

    daggasoft

    Joined:
    Jun 29, 2013
    Posts:
    22
    Thanks, can you tell me how to narrow this down? Not sure I follow.

    I have done some further research, stepping through the game in the editor. What I've noticed is it will preserve items I manually place into the world, but will not preserve the same object that I instantiate via a script.

    So I have the manual object called Coin, then the instantiated ones called Coin(Clone). The Clones die on level load.

    When I pause the game and click on both objects they look identical to me in the inspector. They both have the tag assigned. Wondering if there's something special preventing the clones from being maintained? I really dont know.
     
  4. Deleted User

    Deleted User

    Guest

    For example, if you add something like Debug.Log("Calling DontDestroyOnLoad") inside your foreach loop, then you can see in the Console if its called as many times as you're expecting. Or you could use the debugger in MonoDevelop (or is that what you mean when you say you're stepping through in the Editor?)
     
  5. daggasoft

    daggasoft

    Joined:
    Jun 29, 2013
    Posts:
    22
    OK so this clearly isn't working because I have it in my start(){} section of my paddle's script, so obviously this only runs at the very beginning of the game because I only spawn the paddle once per game.

    I moved it to the update section just before where I'd load a new level (if no bricks left load level) and it works now. Thanks!
     
  6. daggasoft

    daggasoft

    Joined:
    Jun 29, 2013
    Posts:
    22
    Also can you confirm a misconception I have on dontdestroyonload. I have some persistent objects, like the background and walls. I only have to call this once at the beginning of the game and these objects will persist through new level loads, or do I have to call is before each new level load?
     
  7. Deleted User

    Deleted User

    Guest

    You only need to call it once on an object to make it persistent. In HyperBowl, I call DonDestroyOnLoad on everything in my first scene that I will want to reuse in the rest of the game - HUD, pause menu, scoreboard, bowling ball (and maybe pins), ball rolling and collision sounds...and then I never call it again after exiting that first scene (and I never enter that first scene again so I don't have to worry about those objects getting duplicated)