Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by jrhowa · Aug 24, 2016 at 08:58 AM · unity 5debug

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · Aug 24, 2016 at 09:10 AM 1
Share

Why

?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Bunny83 · Aug 24, 2016 at 10:27 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 24, 2016 at 10:30 AM 0
Share

ps: You also should differenciate between an object address and a variable address. A GameObject reference variable has itself a location in memory regardless what that reference references. But before we go more in detail you should tell use what's the point of that.

avatar image jrhowa · Aug 24, 2016 at 01:54 PM 0
Share

@Bunny83 To setup more info on what I was trying to do is. Let's say I Instantiate 10 objects from the same prefab, I want to in some way find what object I'm looking at. You ended up telling me what I needed even tho the way I asked it was a bit off the mark. In other systems, I would do this by looking at the pointer to memory.

In short, GetInstanceID was the thing I was looking for. Just ended up going the long way around to find it.

Thanks.

avatar image Bunny83 jrhowa · Aug 25, 2016 at 12:50 PM 0
Share

Actually Debug.Log has a two parameter version which allows you to pass any object derived from UnityEngine.Object along. This will be highlighted when you click on the log in the console if the object is a scene object or an asset.

Furthermore any public variable (which shows up in the inspector) to other objects can be clicked on and the referenced object is highlighted

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Submitting bug reports 0 Answers

Unity 5.6 - HoloLens debug build exits with code 0x80000003 1 Answer

Android build error: unrecognized pixel format 264 0 Answers

Could the game crash be a GC problem? 0 Answers

Tracking Player x Position for different things to happen not working 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges