• 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
8
Question by shdes161 · Sep 27, 2016 at 07:56 PM · gameobjectscomponentspersistenceunique-identifiers

Need a persistent unique id for gameobjects

So, let me clarify my exact issue here: I am making a system in which certain GameObjects will get a unique id, that is not only unique, but persists over Unity Editor updates (I'll explain the reason I mention this specifically), game quits/etc.

I already know how to:

  1. Save data to file to persist over game quits/plays.

  2. Create unique IDs with Guids.

For now I have been manually setting new ID's with a custom context menu and method that simply creates a new Guid.

I have tried/combined these two ideas (I did try many others but these two were the closest to being successful):

http://answers.unity3d.com/questions/487121/automatically-assigning-gameobjects-a-unique-and-c.html http://joshuasmyth.maglevstudios.com/post/Generating-Persistent-Unique-Ids-in-Unity3D2 (I event tried copying the code line for line in case I typed something incorrectly and it still would not work...)

This semi worked. It created new IDs for every new gameobject but since unity's build in InstanceID changes everytime the unity editor reloads/scene loads/etc this broke and regenerated the IDs, making persisting IDs impossible.

Is there any simple solution or code that would either make IDs different as soon as this "UniqueID" component is added to a GameObject or a way to keep track of these objects (without a decrease in Unity Editor performance) and recreate IDs if any match/gameobject duplicated?

Thank you for all the help!!

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 flashframe · Sep 28, 2016 at 01:03 AM 0
Share

How about placing a "PersistentID" component on each gameobject that needs a persistent ID and storing its ID as a public (i.e. serialized) int?

That way you can assign it a unique id just once, after doing a check to see if any other gameobjects in the scene have the same ID (using FindObjectsOfType), and then you'll have a permanently stored ID that won't change between scene loads.

 using UnityEngine;
 using System.Collections;
 
 public class PersistentID : $$anonymous$$onoBehaviour {
 
     public int persistentID;
 
     void SetID()
     {
         persistentID = GetInstanceID();
         //etc
     }
 
 
 }

11 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by UDN_46c91c6c-aeb1-471f-87fb-917fc1db02c9 · Jun 19, 2019 at 09:41 AM

Probably I am late, @shdes161

You can use System.Guid When generating an ID it's guaranteed to be unique. The only thing you need to do is to generate and store it in the gameObject.

To do this you can create in a component a public string field (Since unity doesn't serialize properly the GUIDs) like this:

 public string mGuid = System.Guid.NewGuid().ToString(); //Unique persistent ID

And that's all you have to do to have a unique id that is persistent. Yet, remember that when you clone an object the GUID will remain so it will be great to create an editor component that takes care of updating the value.

Comment
Add comment · Show 1 · 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 · Jun 20, 2019 at 10:13 AM 0
Share

Uhm the question already stated that he already uses the Guid type to generate a unique ID. However the actual question / problem is how and when a new Guid need to be generated and when not. In the editor you can not really distinguish between loading a serialized object and duplicating an already serialized object. So you either get duplicated Guids when you clone objects or it will not be persistent and you generate a new one every time.


So your answer doesn't really answer the question how to actually solve this issue. I've created this class which I already mentioned above in a comment. It works as expected, though it can't cover all cases. However it should handle duplication and instantiation properly.

avatar image
0

Answer by Mike_17 · Aug 21, 2019 at 01:01 PM

A very late answer from a distant future (sorry ^^')

There is one ID contained in every component that is constant between editor instances, it's the Local Identifier in File.

In Unity version 2018.1 this was added : https://docs.unity3d.com/2018.1/Documentation/ScriptReference/AssetDatabase.TryGetGUIDAndLocalFileIdentifier.html

Thanks to this we can now access local ID's but beware when handling multiple loaded scenes as I'm not sure this id will be unique between scenes.

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 Ash-Blue · Mar 23, 2020 at 02:36 AM

I actually wrote an open-source Unity package to solve this specific problem called Fluid Unique ID. It checks across scenes for duplicate and null IDs with a GUI. It also supports handling prefabs and no coding required to use it.

https://github.com/ashblue/fluid-unique-id

alt text


banner.png (48.1 kB)
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
2

Answer by AntonPetrov · Apr 23, 2020 at 02:42 PM

The problem of persistent identifiers solved by Unity here: guid-based-reference

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
-1

Answer by AtomsInTheVoid · Sep 25, 2020 at 05:52 AM

I feel like everyone's overcomplicating this...

Why not just use the DateTime.Now as it's unique ID?

Works for me, although perhaps not if your blasting out several objects in the exact same ms. For my purposes I will never have that problem.

Comment
Add comment · Show 1 · 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 Ziplock9000 · Sep 25, 2020 at 07:51 AM 0
Share

Lots of reasons.

A few are: Not unique across threads, process, computers etc Often not enough resolution, this repeating ids

It's not about blasting lots out.. it can just be two at a similar time.

In fact, time is already used as a small part of UUID/GUID generation..

There's a reason why UUIDs and GUIDs are in use as an industry standard in the first place.

  • ‹
  • 1
  • 2
  • 3
  • ›

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

72 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 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 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

Trading card game uniqe id generation 2 Answers

Attach the ObjectManipulator MRTK component in runtime to instantiated GameObject? 0 Answers

Opeartions on GameObjects before scene is loaded 0 Answers

Get a GameObject from a component 1 Answer

handle gameobject Component in c# 3 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