Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
2
Question by softrare · Feb 05, 2012 at 05:36 PM · gameobjectprefabpathfind

Find prefab path of a gameObject?

Hi!

Does anyone know how I could find the prefab path of a gameobject?

Something like (go is an ordinary gameObject):

 string path = findPrefabPath(go);
 GameObject newGO = GameObject.Instantiate(Resources.Load(path));

I know this code sample here does not make so much sense, but I really need this function as it would be used in the sample above.

I found this: http://unity3d.com/support/documentation/ScriptReference/PrefabType.html but it is not all I need, this would only give the information if an object has some kind of prefab connected to it. I need the path it is connected to (or even disconnected from).

Is it possible?

Thanks in advance!

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

6 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Keep Silence · Oct 16, 2013 at 07:05 PM

try this

Object parentObject = EditorUtility.GetPrefabParent(obj); string path = AssetDatabase.GetAssetPath(parentObject); Debug.Log("prefab path:" + path);

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 Aldrick · Oct 08, 2015 at 10:07 AM 0
Share

Yes,that's what I figured out myself.The GetPrefabParent method. But why doesn't GetPrefabObject work?

avatar image Cambesa Aldrick · Oct 08, 2015 at 04:48 PM 1
Share

For everyone who needs this, EditorUtility.GetPrefabParent(obj) is obsolete use PrefabUtility.GetPrefabParent(obj) ins$$anonymous$$d.

avatar image OVIOAli Cambesa · Jan 21, 2021 at 11:35 AM 1
Share

This is also obsolete now. Try use PrefabUtility.GetCorrespondingObjectFromSource()

avatar image
2

Answer by Cameron 3 · Feb 05, 2012 at 06:18 PM

Softrare,

You would use the AssetDatabase class. Check the scripting ref, there's a lot of good stuff in there. IE:

 string myPath = AssetDatabase.GetAssetPath( myGameObjectOrPrefab );
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 softrare · Feb 05, 2012 at 06:26 PM 0
Share

Thank you! Seems logical, but strangely this function returns "" (empty string) oO http://unity3d.com/support/documentation/ScriptReference/PrefabType.html returns DisconnectedPrefabInstance. So the path should really be possible to grasp right?

avatar image Kryptos · Feb 05, 2012 at 08:22 PM 0
Share

Try using EditorUtility.GetPrefabParent() and then ask the AssetDatabase about the root prefab. Not sure this will work with DIsconnectedPrefabInstance though.

avatar image
1

Answer by Sky-Foxy · Nov 02, 2016 at 03:46 PM

If you need find object in Prefabs, Material, Scene, by GUID https://gist.github.com/AndreySkyFoxSidorov/977e5b0a91b3bc3e2acf53917e32f410

Click Right-click select "Find GUID in Prefabs Material Scene" the object to find in Prefabs, Material, Scene, by GUID.

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 Berenger · Feb 05, 2012 at 06:02 PM

Unless you need the exact prefab setups, you can always instantiate an object from a GameObject (prefabs are just that) from the scene :

 GameObject newGO = GameObject.Instantiate(go);
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 softrare · Feb 05, 2012 at 06:18 PM 0
Share

Yes, I know that. But indeed I need the exact path. Thanks anyway!

avatar image
0

Answer by IgorAherne · Jun 30, 2017 at 05:33 PM

Better use this

 PrefabUtility.FindPrefabRoot(_targ.gameObject);
 string path = AssetDatabase.GetAssetPath(prefabParent);

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
  • 1
  • 2
  • ›

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

14 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

Related Questions

Disable/Enable game object by clicking instantiated prefab during runtime 0 Answers

Finding a prefab in after respawn 1 Answer

Affect All Instances Of A Prefab 3 Answers

All the objects in my script choose the same target. 2 Answers

Acessing scripts in other game objects with prefabs, without using GameObject.Find 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