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 ivanTxd · Oct 16, 2020 at 02:24 AM · gameobjectpersistentunique-identifiersidentify

How to get some unique identifier for GameObjects?

I need to resolve the following issue:
I was saving in a Json the IDs of some GameObjects, using the GetInstanceID () method; then I could load them using a custom function. But when making the final tests, when changing the scene or doing the build, I was surprised that the IDs of the GameObjects changed, therefore the load with the custom function was wrong since it did not find the IDs previously saved.
What is needed is to obtain some type of ID, code or whatever from a GameObject to identify it, and that this ID, code or whatever never changes, is that possible? Additional note:
I tried to use generation with GUIDs, but where would I store that ID? I have more than 1000 GameObjects.
I'm thinking of generating the IDs manually, attaching a script to each GameObject at runtime, this would work as long as no GameObjects are added, removed, disabled.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Namey5 · Oct 16, 2020 at 06:09 AM

If you want the identifiers to be consistent, then it would be best to generate and serialize them in the Editor. The easiest way of doing so would be to have the GUID as a serializable field on a script;

 using System;
 
 public class UniqueID : MonoBehaviour
 {
     //I would suggest adding some kind of ReadOnly attribute/inspector to this such that you can see it in the inspector but can't edit it directly
     [SerializeField] private string m_ID = Guid.NewGuid().ToString();

     //If you need to access the ID, use this
     public string ID => m_ID;
 
     //This allows you to re-generate the GUID for this object by clicking the 'Generate New ID' button in the context menu dropdown for this script
     [ContextMenu("Generate new ID")]
     private void RegenerateGUID () => m_ID = Guid.NewGuid().ToString();
 }
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 ivanTxd · Oct 16, 2020 at 02:02 PM 1
Share

Thanks, this generates unique IDs, but the problem is that, every time you give Play in Unity's Editor, new IDs are generated, I need it to be the same always. @Namey5 alt text

id-guid-change.png (72.7 kB)
avatar image Namey5 ivanTxd · Oct 16, 2020 at 11:20 PM 0
Share

$$anonymous$$y bad, the Unity serializer probably doesn't work with the Guid class, however you can easily convert it to something that does - a string. I've updated the original answer.

avatar image SamFernGamer4k ivanTxd · Jan 21, 2021 at 12:13 AM 0
Share

It is not meant to be the same. It is meant to be different for every script. You need to generate only if it hasn't already.

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

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

Related Questions

How to change onValueChanged target GameObject in a Slider during runtime? 1 Answer

Scene gameobject public variables assigned by script in editor doesnot persist - but manually entered values in editor UI persists 1 Answer

Find GameObject with value within script 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Not clear how Application.LoadLevel works 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