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
0
Question by hanford · Dec 06, 2011 at 09:53 AM · gameobjecteditorprefabnamesavegame

Get the name of an instance's prefab at runtime?

For savegame purposes I want to be able to access the name of an in-scene prefab's master-prefab name.

I want to do this so that the savegame code knows which resource to load, using the Resources.Load() function.

I don't want to rely on the GO's name, since that could be renamed in the scene and break the link. I also don't want to have to enter it by hand for all my GOs, since that would be error-prone.

Since prefab instances are linked to their masters (at least in the editor), is there a way to access this information programatically?

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
3

Answer by Statement · Dec 06, 2011 at 10:29 AM

There is no way that I know of. Maybe you could programatically add a utility component each time you instantiate a prefab though Resources.Load that store the name of the prefab?

Something like this?

 function LoadPrefab(var path : String) : GameObject
 {
     var obj : GameObject = (GameObject)Resources.Load(path);
     var tracker : PrefabTracker = obj.AddComponent(PrefabTracker);
     tracker.path = path;
     return obj;
 }
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 hanford · Dec 06, 2011 at 11:49 AM 0
Share

Well that would work when using resources.load() because by that point we already know the path.

$$anonymous$$y issue if for placing items in a scene that might go into a player's inventory. For example, on level 1, the player may pickup a Gun prefab called LazerGun001. It's hand-placed in the scene and doesn't use Load().

In level 2 he saves the game while holding this gun. Now, we can't assume LazerGun001 exists in level 2. it may only be found in level 1.

so at the point of savegame, at a point where resources.load() has never been called, savegame needs to know the name of the resource so that loadgame can load the right gun. There's a few solutions to this:

  1. when dragging the LazerGun001 prefab from resources to the scene, don't rename it and use gameObject.name for the path.

  2. make the resources prefab have a "resourceName" variable that I hand-set per master prefab, and make it match the asset path.

  3. Same as #2, but make it happen automatically.

  4. something clever I'm not thinking of.

Number 1 is out of the question for me. Too much opportunity for messing it up and makes subfolders harder.

Number 2 is prone to user error but much less so. This is my best working bet so far.

Number 3 is what I'm hoping for at the moment.

Thanks for the help!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why my prefab is auto changing? 3 Answers

Very Slow (hang) gameobject TO prefab operation with lot of child objects 0 Answers

How can I tell what prefab a GameObject belongs to? 2 Answers

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

Automatically update prefab instance 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