• 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
3
Question by falconfetus8 · Jul 05, 2013 at 01:14 AM · id

Automatically assigning GameObjects a unique and CONSISTENT ID: any ideas?

I'm trying to make a system that automatically remembers data between two scenes so that the scenes appear to be "persistent"; IE: If I open a chest in one scene, hop over to another scene, and then hop back to the original scene, the chest should still be open.

The current system I have in place is working BEAUTIFULLY, but there's a major catch: in order for it to work, I need to assign each persistent object an ID number, and this number has to be the same each time the scene is loaded up. At the moment, I'm just assigning this ID number by hand in the inspector when I place each individual object. However, I would like to have a system that automatically assigns each object a unique ID for me.

So far, I've tried doing something similar to this:

 public class PersistenceRememberer : MonoBehaviour {
     
     public static int nextID = 0;
     
     private int myID;
     
     //Events
     
     void Awake(){
     
         myID = nextID;
         nextID++;
     }
 
     void OnLevelEnd(){
         //Clear the nextID
         nextID = 0;
     
     }
 
 }

However, this has a major flaw in it: each time a new scene is loaded, the objects end up getting different IDs.

So, I decided to come here for ideas. How can I automatically assign an ID to an object in such a way that it will always be the same, even when the player hops back and forth between scenes?

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

8 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by PeterMorris · Oct 27, 2016 at 08:02 AM

I'd use a GUID. Random integers aren't guaranteed to be unique

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
avatar image
0

Answer by emanmomot · Oct 04, 2017 at 09:34 AM

In the scene file, every gameobject has a unique identifier that is persistent. You can access it like this:

PropertyInfo inspectorModeInfo = typeof(SerializedObject).GetProperty("inspectorMode", BindingFlags.NonPublic | BindingFlags.Instance); SerializedObject serializedObject = new SerializedObject(unityObject); inspectorModeInfo.SetValue(serializedObject, InspectorMode.Debug, null); SerializedProperty localIdProp = serializedObject.FindProperty("m_LocalIdentfierInFile"); //note the misspelling! int localId = localIdProp.intValue;

The only catch is that this only works in the editor, so you have to serialize it when you want to build your game.

Answer found here: https://forum.unity.com/threads/how-to-get-the-local-identifier-in-file-for-scene-objects.265686/

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
avatar image
0

Answer by SgtOkiDoki · Dec 08, 2018 at 06:55 AM

I end up with this =>

 public float GetID(Transform obj)
     {
         float a = 0;
 
 
 
         System.Text.StringBuilder s = new System.Text.StringBuilder();
         s.Append(obj.name);
         Transform parent = obj.parent;
         a += obj.GetSiblingIndex();
         while (parent != null)
         {
             s.Append(parent.name);
             a += parent.GetSiblingIndex();
 
             parent = parent.parent;
         }
 
         string name = s.ToString();
         char[] c = name.ToCharArray();
         foreach (var item in c)
         {
             a += (int)item;
         }
 
         Vector3 pos = obj.transform.position;
         Vector3 rot = obj.transform.eulerAngles;
         Vector3 scale = obj.transform.localScale;
 
         a += pos.x;
         a += pos.y;
         a += pos.z;
 
         a += rot.x;
         a += rot.y;
         a += rot.z;
 
         a += scale.x;
         a += scale.y;
         a += scale.z;
 
         a += obj.childCount;
 
         return a;
     }
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
  • ‹
  • 1
  • 2

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 People are following this question.

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

Related Questions

Instantiate ID 2 Answers

Photon Problem 0 Answers

how to create ID to script ?? 0 Answers

store contact with last before destruction 3 Answers

Google Play test in-app purchase non-blank order ID. 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