How to get unique ID for ScriptableObjects?

Is there a way to get a consistent, unique hash/id for a ScriptableObject? I’ve tried using GetInstanceID but those seem to change occasionally, not every time the game starts, just some times.

Since I can’t write a reference of a ScriptableObject to a file I was trying to just save an ID from that object so that I can then look up the ScriptableObject using that ID.

Thanks

How many scriptable objects are you needing?

If it isn’t a lot, you could create a variable int called ID, make it public, assign it in the inspector, and you would be done.

You can use the same approach I used over here in my UUID component. This should work for scriptable objects as well. However since ScriptableObjects are standalone classes it’s difficult to create a system for different classes. However it’s of course possible. One solution could be to create a base class from which you derive all your scriptable objects.

Note that it might not be bullet-proof but should handle most common operations including duplication and hot reloading.

This approach also has the advantage that you can use the dictionary to look up instances by it’s GUID as long as the objects are loaded

Same concept as Bunny83 but extending the scriptable object.

This has been working reliably for me with duplication and creating new instances.

Core concept is Guid.NewGuid().ToString(); and tracking duplicates.

I read over at Unity’s GUID repo that they had a problem tracking the string, rather than using a byte array, which might be an improvement over my implementation. I might add that eventually. :slight_smile: