• 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 travis_unity746 · Jun 11, 2021 at 05:54 AM

I stumbled on this thread trying to grapple with the same problem the OP has outlined, and after a lot of trial and error with the proposed solutions in this thread, I've finally got it to work - for anyone looking for a full guide on how to accomplish this, I've put one together here: https://bdts.com.au/tips-and-resources/unity-how-to-save-and-load-references-to-gameobjects.html

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 DSivtsov · Feb 16 at 09:12 AM 0
Share

@travis_unity746 Good article and code, but have little issue - it doesn't store values GUID in case when the GameObject in the scene (instantiated prefab and unpack complete). And Update called very often especial in Play Editor mode. Ultimately I stayed on next variant base on your Awake but modificated, It generate newGuid only then attached to Gameobject, and also will "regenerate" it if will be detected in editor (load scene and etc.) that value is empty. Therefore it will run only once when you start/stop game. EDIT " also will "regenerate" it if will be detected in editor (load scene and etc.) that value is empty" Finally I decided to exclude the problem regeneration by use attribute [ReadOnly] for field guid. It's more safe.

 [ExecuteInEditMode]
 public class StoreObjects : MonoBehaviour
 {
     private StoreGame storeGameManaegr;
     [ReadOnly]
     [SerializeField] private string guid;
 
 #if UNITY_EDITOR
     /// <summary>
     /// Create a new unique ID for this object when it's created
     /// </summary>
     private void Awake()
     {
         if (!Application.isPlaying && String.IsNullOrEmpty(guid))
         {
             Debug.Log("StoreObjects : Awake() - Generated NewGiud");
             //Undo.RecordObject(this, "Generated NewGiud");
             guid = Guid.NewGuid().ToString();
             PrefabUtility.RecordPrefabInstancePropertyModifications(this);
         }
     }
 #endif
 }
avatar image Ziplock9000 · Feb 16 at 03:43 PM 0
Share

It's generally bad practice to post links to solutions on external websites as they can be restructured or are removed completely. Special interest knowledge boards are littered with these sort of broken links. The better practice to include the link AND copy the data over as a post too.

  • ‹
  • 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

Component references when GameObject destroyed? 2 Answers

How to add a component to all selected objects? 1 Answer

Connecting two blendshapes 1 Answer

Trading card game uniqe id generation 2 Answers

Attach the ObjectManipulator MRTK component in runtime to instantiated GameObject? 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