Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Identifying objects in the scene after changing levels

Discussion in 'Scripting' started by andygeers, Jun 27, 2009.

  1. andygeers

    andygeers

    Joined:
    Jun 13, 2009
    Posts:
    30
    I'm trying to write some kind of global state handler that maintains the state of scenes when you switch between them. Essentially it needs to keep track of what has been removed from each scene, so that when you then reload that scene on a future occasion (having visited other scenes in the mean time) it can re-remove them.

    The problem I'm having is how you identify each item. The GetInstanceID method returns unique values that are different each time you load the scene. The objects also seem to load in an arbitrary order (or at least 'FindObjectsOfType' seems to return them in an arbitrary order) so I can't do it based on that. The names aren't necessarily unique (you may have two instances of the same thing).

    Any ideas?
     
  2. andygeers

    andygeers

    Joined:
    Jun 13, 2009
    Posts:
    30
    Hmm... Well it's looking like the *order* of the Instance IDs is consistent, even if the values themselves aren't. So I'm going to try using that for a while and see if it works.
     
  3. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    I wouldn't rely on the instance id. The only thing it should be used for is to compare two object instances or to identify a specific instance. Since all instances are removed and re-created when you unload/load a level I wouldn't rely on the way they are assigned.

    Instead, why not simply use the game object's name? Each game object has a name property that you can comfortably set in the editor and there's GameObject.Find(name) to get a game object with a given name in the current scene.
     
  4. andygeers

    andygeers

    Joined:
    Jun 13, 2009
    Posts:
    30
    I'd prefer to avoid having to manually keep track of making sure all of the names are unique. By default, all of my objects are loaded in a with a mesh of the same name (they're Blender files are typically the first object in a Blender scenes ends up being called 'Cube' unless you take the time to rename it) and if I can get it working with some other mechanism that is automatically guaranteed to be unique I'd prefer it.
     
  5. BS-er

    BS-er

    Joined:
    May 24, 2009
    Posts:
    115
    I'm quite annoyed by the same issue. I'll be working with partners who will surely give the same name to multiple instances on a regular basis. Instancing prefabs (defaulting to the same prefab name) in the editor will cause a lot of this grief.