Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by lyzard · Jun 11, 2012 at 11:58 AM · hashunique-identifiers

Uniquely identify objects ?

Hi, I'd like to identify objects or components in a unique way, but it seems that GetHashCode() or GetInstanceId() are different each time I build my projet. I know I could setup the name to identify uniquely my objects, but this may lead to other issues. Is there any other easy solution apart from creating custom code ?

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 yoyo · Jan 15, 2014 at 01:39 AM 0
Share

As you discovered, GetInstanceID is transient -- every object has a unique ID, but only for the duration of the Unity (game or editor) session. In the editor environment, depending on use case, AssetDatabase.AssetPathToGUID might be helpful -- this will give you a unique ID per asset file in your project. Note that you only get one ID per asset (e.g. not per component on a prefab), you can't access these at runtime, only in editor code (though you could store them away for use later), and if you delete and regenerate .meta files you will get new GUID's, which will invalidate any ID's you've saved elsewhere.

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by whydoidoit · Jun 11, 2012 at 12:12 PM

Well there are two ways of looking at this:

  • Ensure that the name of an object is unique

  • Create and store a unique identifier for the object

You can create a unique Id using something like this:

    public string Id = Guid.NewGuid().ToString();

GetHashCode() changes as your properties and fields change (probably, depends on the implementation). GetInstanceId() is doing something with the memory address the object is currently at. There is some script here that will make objects have unique names.

Comment
Add comment · Show 2 · 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 lyzard · Jun 11, 2012 at 12:21 PM 0
Share

thanks $$anonymous$$ike, changing the name manually is cumbersome, and automatically unpreferred since for most objects I prefer to keep the exact name I setup. For the other solution, alas I'm not sure where I would store the id since it would entail adding a script dedicated to that, or serializing the pair my own way. If there is no other solution, I'll probably write a custom hashcode method, actually in my case I realized that it may be something as simple as storing the name+position :p thanks for the pointer anyway, I might need that in the future!

avatar image whydoidoit · Jun 11, 2012 at 12:31 PM 0
Share

Yeah I see your point. Be careful on writing a GetHashCode - it has to interact nicely with an Equals override so I understand so that sorts/dictionaries etc continue to work.

avatar image
1

Answer by IsGreen · Jul 31, 2015 at 08:33 AM

You can store index ID using PlayerPrefs. Effectively, Unity ID are different each time.

 using UnityEditor;
 using UnityEngine;
 
 
 [System.Serializable]
 public class UniqueID{
 
     public int ID;
 
     static string key = "UniqueID";
 
     public UniqueID(){
 
         if(PlayerPrefs.HasKey("UniqueID")){
 
             this.ID = PlayerPrefs.GetInt(key);
             PlayerPrefs.SetInt(key,this.ID+1);
             return;
 
         }
 
         PlayerPrefs.SetInt(key,1);
         this.ID = 0;
 
     }
 
 }
 
 
 [CustomPropertyDrawer (typeof (UniqueID))]
 public class UniqueIDDrawer : PropertyDrawer {
 
     public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) {
 
         EditorGUI.BeginProperty (position, label, property);
         position = EditorGUI.PrefixLabel (position, GUIUtility.GetControlID (FocusType.Passive), label);
         int indent = EditorGUI.indentLevel;
         EditorGUI.indentLevel = 0;
         string value = property.FindPropertyRelative ("ID").intValue.ToString();
         Rect IDRect = new Rect (position.x, position.y, 25f + value.Length * 6f, position.height+2f);
         EditorGUI.LabelField(IDRect,value,GUI.skin.box);
         EditorGUI.indentLevel = indent;
         EditorGUI.EndProperty ();
 
     }
 
 }


How to use:

 using UnityEngine;
 using System.Collections;
 
 public class script : MonoBehaviour {
 
     public UniqueID ID;
 
 }


IMPORTANT: If you copy/paste GameObject or component, remember "Reset" the component to create unique ID.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to write Multidimensional Hashes? 1 Answer

Unity 5 - nameHash obsolete? shortNameHash vs fullPathHash 0 Answers

Unique Identifier for both iOS 7 and for older iOS 2 Answers

Check asset bundles compatibility before downloading 0 Answers

How to Setup Openssl & Facebook SDK With Android key HASH on mac OS 10.11.6 1 Answer


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