Using Debug.Log to output the memory address of a GameObject

I would like to get the variable address of the gameobject using Debug.Log rather than just the name of the GameObject. Bonus points for output being in hex.

Thanks for your time.

jrhowa

Managed references don’t directly relates to memory addresses. Where the objects actually are in memory could even change if the GC decides to. So the memory location doesn’t have to be constant during the lifetime of an object. Only pinned objects will stay at the same address, however having a lot pinned object can have a bad impact on the GC as it can’t optimies the memory if objects are locked in place.

Dealing with actual pointers and memory addresses requires you to use unsafe code.

Like tanoshimi said: Why do you need this? You might want to use “GetHashCode()” instead which returns an int value for that specific object. Of course Hash values don’t have to be unique. All objects derived from UnityEngine.Object also have an instance ID (GetInstanceID()).

If you really want to read the memory location, see this SO post.

To display a 32bit integer value as hex string, just use val.ToString("X8"). Of course 64 bit values would need 16 digits.