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 oakus · Nov 26, 2014 at 02:13 PM · editorprefab

PrefabUtility.GetPrefabObject doesn't work!

I am trying to use PrefabUtility.GetPrefabObject to get prefab for scene object. However, when i get object of type "Object" and trying to cast it to game object either

var prefab = PrefabUtility.GetPrefabObject(prefabRoot) as GameObject;

or

var prefab = (GameObject) PrefabUtility.GetPrefabObject(prefabRoot);

I always get null (or InvalidCastException).

Also, when inspecting returned value, it says UnityEngine.Prefab but I've been unable to find this type via Object Browser or IlSpy. I'd really like to get at least name of prefab so I can fetch it myself, but this Prefab object has been useless so far for me.

So far it seems that there is either a bug I didn't get the purpose of this function. Please advise :).

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 26, 2014 at 02:53 PM

Prefabs are a pure edit time feature. At runtime prefabs don't exist. That's why PrefabUtility.GetPrefabObject (or PrefabUtility in general) is an editor function which can't be used at runtime. The concept of prefabs is a bit complicated since there are different types of prefabs.

At runtime there's no way to tell which object might have been cloned from which. At runtime prefabs are just GameObjects which are loaded, but not part of the scene and therefore they don't get any Update or other callbacks. They purely serve as clone / Instantiate source. You can use Instantiate to clone any object derived from UnityEngine.Object. There's no way, by looking at the clone, to tell from which source an object was created. Even at edit time if you use Instantiate the "connection" to the prefab is lost. You have to use PrefabUtility.InstantiatePrefab when instantiating a prefab in an editor script to keep the connection alive. As said this is only for in-editor usage.

Again, at runtime there are no prefabs. All you get at runtime is a bunch of serialized references to those off-scene objects to be able to instantiate (clone) them into the scene.

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 oakus · Nov 27, 2014 at 10:52 AM 0
Share

This is a good answer, however to completely different problem. Of course I am running this function at editor time. I am not a fool to use UnityEditor dll in runtime since it doesn't event build.

That being said, I rephrase the question: I can't get any valid information that could help me deter$$anonymous$$e the prefab object. I am developing an editor tool and I need to show the user which prefab belongs to which object placed in the scene, that is being showed in editor window. I mean, the "Select" button in Prefab section of Unity inspector has to work somehow right? They don't use the same API or what?

avatar image
0

Answer by oakus · Nov 27, 2014 at 03:20 PM

Workaround found with help of Unity staff:

http://forum.unity3d.com/threads/prefabutility-getprefabobject-doesnt-work.282335/#post-1863415

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 Answers

Change angle of camera in prefab preview 1 Answer

How Mark Prefab Dirty? 1 Answer

Can I force z position to match prefab when it's dragged onto scene? 1 Answer

PrefabUtility.InstantiatePrefab not working with PrefabType.ModelPrefabInstance? 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