- Home /
Correct Folder for Resources.Load() ?
hi guys, i am pretty new to unity, and i cant see through the folder structure that is nessecary for a call of Recources.Load("MyPrefab") to not return null but give me a proper instance of a game object (i know that i need to typecast beforehand). The script reference says:
The path is relative to any Resources folder inside the Assets folder of your project, extensions must be omitted.
So does it mean the "Standard Assets" Folder ? (i am using the free version) I am quite confused... if somebody could give me a hint i would be glad. thx in advance.
Answer by whydoidoit · Sep 17, 2012 at 02:23 PM
Assets is the root folder of your project (you don't see it in Unity - it is the owner of everything in the project view).
You can create a Resources folder anywhere and the path for Resources.Load is the path beneath that.
Resources/My Folder
Resources.Load("My Folder/SomeResource")
i just figured it out that moment, thats exactly what i did wrong ! thx ! While we are at that topic do you have a suggestion how i should structure the content of my Assets folder ? At the moment i have $$anonymous$$yProject/Assets/Audio.. and then something like Fonts, Scripts, and so on... but now i realize that i probably need : $$anonymous$$yProject/Assets/Resources.. as well, for anything that i need to load inside the script right ? so basically all my prefabs need to go there...
Your prefabs need to go there if you want to load them at runtime yes - though there are other techniques like having an game object with a script containing all of the prefabs you want - it depends on your design style. Basically anything that isn't in a Resources folder isn't going to make it into the build unless it is referenced in one of the built scenes.
You can have that resources folder anywhere and you can have more than one:
Images/Resources/$$anonymous$$y Folder
Fonts/Resources/$$anonymous$$y Folder
Both of which are accessed in the same way as in my answer (they are kind of compiled together).
I try to keep my projects organised with a root Resources folder because I like to be able to find them all when I need them (to remove something I don't need to reduce the build size normally).
so basically, if i put assets into a scene (e.g i add an audiofile to a gameobject in the scene) then i am good, but if i want to create objects at runtime i need to put them into a resources folder... i think i got that ! The thing with the gameobject, i didnt get that, do mean i should place one gameobject created from the prefab into the scene beforehand? thx in any case, great clarification !
Right well imagine you have a prefab holder script that has a variable for every prefab you want to create. You set this up in the scene then just access it through the variables:
UsefulPrefabs.js
static var current : UsefulPrefabs;
var spaceShip : GameObject;
var bullet : GameObject;
var enemy : GameObject;
var weapons : GameObject[];
function Awake() {
current = this;
}
The you can drop one of those into the scene and use code like:
var myNewWeapon = Instantiate(UsefulPrefabs.current.weapons[1]);
You just need a reference to the prefab to create it at runtime - so it either needs to be loaded with Resources.Load or by in a variable on something in the scene.
Your answer
Follow this Question
Related Questions
Prefab cant istiantiate through recources.load? 1 Answer
Resources.LoadAll on Current Folder Only? 1 Answer
Controlling prefabs from Resources Folder 1 Answer
About to gut my use of the Resources folder. What are the best practices going forward? 1 Answer
Nested prefab in Addressable prefab 0 Answers