- Home /
Eliminating prefabs from resources folder?
I really don't like that you have to place prefabs in resources folder to instantiate from scripts. It makes the project folder structure a mess and lots of scrolling and searching for certain prefabs.
I want to place prefabs in where I want to. Is there any easy workaround to achive this? Something that might work might be to make an empty scene and place prefabs and store their references to some static class's variables so later you can get the prefab from the static class. But it sounds awkward. Can anyone give me a better idea?
Answer by musaranya · Apr 26, 2014 at 06:18 AM
If you keep your assets folder well organized with subfolders it shouldn't be a problem to have all your resources there including the prefabs. I'm not sure you are doing it right but I think the cleanest way to instantiate prefabs is as follows:
in the script from where the prefabs will be instantiated place a public variable of a type that can points to the prefab (a component included in the prefab, usually a Rigidbody, an AudioClip, etc)
then in the inspector panel you should see this as a property of the object which has this script attached: drag your prefab from the well organized project panel to this slot, it should match if the prefab contains a component of the type set when declaring the public variable in the script
and that's all, then in the script code you can instantiate this kind of prefabs without having to have them scattered around (remember that you need to make a cast within the instantiation if you're in C#)
hope it helps
To add to that you can have multiple resource folders throughout your project so rather than having all resources that you're loading by code jumbled in one folder you can create a bit more structure.
Thanks, had to google and re-read your answer a lot to figure out what you are saying but I think I got it o.o I didn't know you could instantiate a prefab by refering components.
Your answer
Follow this Question
Related Questions
About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer
What does Unity actually do when Loading Resources that already exist? 0 Answers
Referencing prefab in code without using editor or Resources.Load? 0 Answers
Resources not loading in APK 0 Answers
Resources.Load() What has to be in Resources Folder 1 Answer