Loading Resources from folder in Project to Hierarchy

It sounds like a really simple question, but it's a problem I'm having and I've been unable to find a response yet.

I'm currently doing a few tests to set up a tower defence game eventually - and I've put a test prefab called "Cube" in a folder called Enemies, in project view. I have tried using the following code:


It's attached to a spawner object which will move to a random x location and create the Cube prefab there. It's quite obvious that the Cube part is the problem - and that's definitely its name.

However, it's in a folder called Enemies, which I assume is the problem. At this point, it's not a specific problem that I have to have everything in folders - but sooner or later this organisation will be essential so I may as well nail it now.

Could anybody please help me with how to reference an object in a folder?

On a side note, how would I have it let me drag the prefab in via the inspector? This would probably be a much easier method.

Thanks very much, Callum.

You must put the items inside the Resources folder (or a subfolder of it)

In your case, /Project/Assets/Resources/Cube will be loaded by Resources.Load("Cube")

If you want to load something in a subfolder of that, it'd be Resources.Load("SubFolder/Cube")

To drag an object into the inspector (which is in fact easier), you would just make a public variable for the type of object

In your case:


public GameObject cube;


var cube : GameObject

You'd then just drag and drop into the inspector

Note - you don't have to make it GameObject, it can be any type you have in your prefab, including scripts, AudioSource, etc etc