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 Sendatsu_Yoshimitsu · Sep 07, 2014 at 09:26 AM · c#serializationguid

How do I get a reference to a gameobject from its GUID?

I'm looking for a way to assign each NPC a globally unique identifier that will persist from scene-scene and from session to session. GUID seems to be the way to go, and I get that I can create and assign a GUID via:

 public System.Guid uniqueCharacterIdentifier;
 uniqueCharacterIdentifier = System.Guid.NewGuid ();

I also gather that the simplest way to serialize my IDs would be to write it with .ToString(), and convert it back via uniqueCharacterIdentifier = new Guid(guidAsString);

Where I'm stuck is this: given the GUID for an NPC I want to retain a reference to, how do I actually get that reference? Is there something in the API that converts a guid to the object it's assigned to?

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 Cherno · Sep 11, 2015 at 12:34 AM 0
Share

If a script holds a reference to the NPC GameObject, have it also hold a reference to that GameObject's GUID. $$anonymous$$ake sure that this reference gets saved. After loading, cycle through all GameObjects in the scene and access their GUIDs until you get the right one, and re-establish the GameOject variable reference.

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by ZenithCode · Sep 07, 2014 at 01:08 PM

Get the path from the guid by using: AssetDatabase.GUIDToAssetPath()

Then load asset by using AssetDatabase.LoadAssetAtPath()

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 Sendatsu_Yoshimitsu · Sep 07, 2014 at 09:49 PM 0
Share

I'm working on implementing this now- based on the unity docs, I'm doing:

 string test =AssetDatabase.GUIDToAssetPath (uniqueCharacterIdentifier.ToString());
         testCharacter= AssetDatabase.LoadAssetAtPath (test, typeof(GameObject)) as GameObject;

Debug.log statements confirm that unique IDs are being generated, but the second line isn't working- testCharacter remains blank after this runs. Is there an obvious error I'm perpetrating?

avatar image Andy-Block · Sep 10, 2015 at 08:11 PM 0
Share

But AssetDatabase can only be used in editor, right?

avatar image
2

Answer by BMayne · Sep 11, 2015 at 12:30 AM

Hey,

If you are looking to use the guid at runtime you are out of luck with Assetdatabase, as it's Editor only.

What you need to do is create a manager; staic or not. Your NPCs would add themselves On Enable. Then when you want one by ID you just ask your manager to look over all NPCs and return the one with the correct ID.

When you say you are using GUID from system that is a bit overkill. Your prefab will not be unique since you said one might be in each scene. In your case you should use the type of class; if all inherit from a base class or an enum. An enum that would be much easier to work with. A guid is not easy to follow.

Cheers,

Side note I will add a code example later but it's a nightmare to type with auto correct on a phone

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 Leith_Ketchell · Oct 13, 2018 at 11:34 AM

Why use an enumeration? You can consider your index into your guid collection as an enumeration.

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

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

Distribute terrain in zones 3 Answers

Big Problem Serializing 0 Answers

Multiple Cars not working 1 Answer

Instantiating Prefabs through Editor Script 1 Answer

How do I keep references to unique NPCs between scenes? 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