Persistent Unity object id across session restarts?

Does anyone have any ideas on how to make a persistent id for an object that is the same every time Unity3D is restarted?

If I use GetInstanceID() or GetHashCode() they both change after exiting and opening Unity. I’ve thought about using each object’s position, but there can be times when two objects are at the same spot.

What I’m trying to do is make an editor script that allows me to color code objects in the hierarchy window, meaning I have access to a guid and rect.

I’ve googled this ever way possible but could not find a solution. Thank you!

You could just use the objects full hierarchy path (name) or a function of it.

1 Like

Hmm, that may work. Any idea on how I would go about doing that? Wouldn’t there be a problem of duplicate names?

Thank you!

You could also write a component that generates an integer (that hasn’t been used within the context) or an GUID and serializes the value. Due to Unity’s serialization system this would serialize and de-serialize properly and thus be consistent (unless you mess with it manually).

Yes good point, you could probably include the sibling index also to help avoid duplicates (thought some naming could cause coincidental duplicates.

Getting the full name can be built with:

 public static string GetFullPathName(this GameObject obj)
    {
        string path = "/" + obj.name;
        while (obj.transform.parent != null)
        {
            obj = obj.transform.parent.gameObject;
            path = "/" + obj.name + path;
        }
        return path;
    }

With sibling index:

    public static string GetFullPathName(this GameObject obj)
    {
        string path = "/" + obj.name + obj.transform.GetSiblingIndex();
        while (obj.transform.parent != null)
        {
            obj = obj.transform.parent.gameObject;
            path = "/" + obj.name + path;
        }
        return path;
    }

But as suggested by Suddoha there are a number of other more reliable methods.

1 Like

Thank you. This system ( the second one using indexes) is less invasive for my purposes and it works great!

1 Like

I’m back. If I have an object, that has children, and I duplicate it then I run into the problem of receiving duplicate names, ids. The sibling index only works in the context of one object. If I have two objects, and their children are named the same, then I will end up with duplicate names.

I’m using the names as unique keys in a hashtable, so I end up receiving a duplicate key error.

Maybe I could set all objects to have a parent that is invisible and then use the sibling index to make every object unique? That is a little more invasive than I want, but I could only do it when saving the colors; the only problem is that I would have to loop through all objects and set them to have the same parent, and then go back and unset the parent.

If you have any more ideas on how to always get a unique id for every object I would forever be thankful. Thanks!

How about just changing the use of sibling index to be included at all levels. That example code only ads the sibling index to the bottom most node and not the nodes higher up the hierarchy.

Perhaps this will work:

public static string GetFullPathName(this GameObject obj)
{
    string path = "/" + obj.name + obj.transform.GetSiblingIndex();
    while (obj.transform.parent != null)
    {
        obj = obj.transform.parent.gameObject;
        path = "/" + obj.name + obj.transform.GetSiblingIndex() + path;
    }
    return path;
}
1 Like

Ahhh. That works on all levels now. There are no more duplicates. Every time I have tried to uniquely identify gameobejcts I have run across this problem, and you just solved it once and for good, thank you.

I hope other people will benefit from this! I have tried searching google every which way, and I could never find a reliable method to accomplish this.

If everything keeps working, then I shouldn’t have to ask you for any more help. I really appreciate the time you have taken to answer my questions.

1 Like

This is better than GetInstanceID and the hierarchy path

3 Likes