• 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
4
Question by tmdchi · Sep 05, 2012 at 09:02 AM · prefabassetpath

Is it possible to get a prefab object from its asset path?

For instance, I want to be able to instantiate a gameObject from a prefab stored in "Assets/Prefab/Items/Key_yellow.prefab". Thank you.

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
10
Best Answer

Answer by Kryptos · Sep 05, 2012 at 09:32 AM

Loading Resources at Runtime is what you are looking for.

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 tmdchi · Sep 05, 2012 at 09:58 AM 0
Share

Thank you, it's working :)

avatar image
12

Answer by schlenger · Jan 03, 2020 at 08:02 AM

For everybody new: The answers above weren't working for me with the Unity versions from 2019 and on.

Yes, you might want to check the Docs at https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html

Here it says

you simply create a new folder inside the Project View , and name the folder “Resources”. You can have multiple Resource Folders organized differently in your Project. Whenever you want to load an asset from one of these folders, you call Resources.Load()



- So, you have to move your files e.g. to Assets/Resources/ - let us assume your prefab is called Cube.prefab it is now at Assets/Resources/Cube.prefab. - To load it programmatically, you now can call Resources.Load("Cube"); (Note: You neither need the Resources prefix, nor the .prefab ending) - Instantiation is the same as above: GameObject.Instantiate((UnityEngine.Object) Resources.Load("Cube"), Vector3.zero, Quaternion.identity);

Long story short:

  private UnityEngine.Object LoadPrefabFromFile(string filename)
  {
      Debug.Log("Trying to load LevelPrefab from file ("+filename+ ")...");
      var loadedObject = Resources.Load("Levels/" + filename);
      if (loadedObject == null)
      {
          throw new FileNotFoundException("...no file found - please check the configuration");
      }
      return loadedObject;
 }
     
  // Called via:
  var loadedPrefabResource = LoadPrefabFromFile("Cube");
  Instantiate(loadedPrefabResource, Vector3.zero, Quaternion.identity);


@tmdchi you may want to have a look

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 adriandevera · May 04, 2020 at 10:13 AM 1
Share

This worked perfectly for Unity 2019. Thanks!

avatar image redeamed · Jul 27, 2020 at 12:38 AM 0
Share

thank you sir. I was making the mistake of still including the full path when I was trying to do this.

avatar image
9

Answer by MadDave · Sep 05, 2012 at 09:09 AM

 UnityEngine.Object pPrefab = Resources.Load("Assets/Prefab/Items/Key_yellow"); // note: not .prefab!
 GameObject pNewObject = (GameObject)GameObject.Instantiate(pPrefab, Vector3.zero, Quaternion.identity);
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 Kryptos · Sep 05, 2012 at 09:31 AM 1
Share

Won't work. Because the prefab is not inside a Resources folder.

avatar image tmdchi · Sep 05, 2012 at 09:58 AM 0
Share

Ok, but you headed me in the right direction. Thanks!

avatar image Kryptos · Sep 05, 2012 at 10:42 AM 0
Share

True. So @maddave deserves a +1.

avatar image
5

Answer by Nit_Ram · Feb 08, 2020 at 05:13 PM

You don't have to use the ressource folder as long as you want to use this only in the editor, not at runtime.

Here is a little script that finds you all prefabs in a specific folder:

 private void GetAllPrefabs()
     {
         string[] foldersToSearch = {"Assets"};
         List<GameObject> allPrefabs = GetAssets<GameObject>(foldersToSearch, "t:prefab");
     }
     
 public static List<T> GetAssets<T>(string[] _foldersToSearch, string _filter) where T : UnityEngine.Object
     {
         string[] guids = AssetDatabase.FindAssets(_filter, _foldersToSearch);
         List<T> a = new List<T>();
         for (int i = 0; i < guids.Length; i++)
         {
             string path = AssetDatabase.GUIDToAssetPath(guids[i]);
             a.Add(AssetDatabase.LoadAssetAtPath<T>(path));
         }
         return a;
     }
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
4

Answer by FlightOfOne · May 11, 2018 at 07:22 PM

This is what worked for me (unity 2017.3)

 UnityEngine.Object prefab = AssetDatabase.LoadAssetAtPath("Assets/Prefabs/PlayerController.prefab", typeof(PlayerController)); 
 PlayerController player = Instantiate(prefab) as PlayerController;



Comment
Add comment · Show 5 · 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 eduardonunesp · Dec 16, 2018 at 12:58 AM 0
Share

Still working on Unity 2018.3

avatar image alrightheresmyname · Apr 21, 2019 at 07:50 PM 0
Share

No longer works. Use resource path ins$$anonymous$$d. ex.
UnityEngine.Object pPrefab = Resources.Load("Prefabs/PrefabName");

make sure to place the Resource folder inside your Assets folder, then place any needed files in resource folder.

avatar image FlightOfOne alrightheresmyname · Apr 22, 2019 at 03:43 PM 0
Share

Strange.. I was just using this the other day. Still working for me on 2019.1

avatar image Klamore74 · Mar 25, 2020 at 11:52 AM 1
Share

This works perfectly but we aware: works only in the unity editor, so you cannot use it in the build. This will generate an error at build time.

avatar image sezgink · Mar 08 at 08:17 PM 0
Share

Still working in 2020.3 and perfectly made the job, thank you for smooth solution

  • 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

20 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

Related Questions

Dressing Room 0 Answers

How to get an object (asset) from his path ? 1 Answer

Saving usermade content 2 Answers

Adding audio to asset bugs out the movement 1 Answer

Why do my prefabs not have an Asset Preview? 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