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 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).
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.
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.