Alternative to Resources.Load ?.

Hello,

i heard that using Resources.Load is a pretty bad idea.
I also heard about AssetBundles but I have no time to check it out yet.

So, I want to load a pre-fab ( which is a weapon ) from specific folder, how should I do that ?

Use Resources.Load. I don’t know where you heard that it’s a bad idea, but it’s fine for this use case; in fact it’s pretty much exactly what Resources.Load is for.

1 Like

It’s not Resources.Load that is the problem. It’s the Resources folder that is the culprit. It can lead to confusion if you’re not aware of the way it is handled by Unity. Starting with the fact that everything in that folder is automatically included in builds of your game even if you never use the assets in it.

https://unity3d.com/learn/tutorials/topics/best-practices/resources-folder

You can safely ignore them until you’re more familiar with Unity.

If you don’t want to use Resources.Load you can use the method mentioned in the Instantiate tutorial and the manual.

https://unity3d.com/learn/tutorials/topics/scripting/instantiate
https://docs.unity3d.com/Manual/InstantiatingPrefabs.html

On the script that would normally load the prefab you add a public field to hold a reference to the prefab and then in the inspector window for the script you drag-and-drop the prefab into the inspector field.

public Transform prefab;

When you want to instantiate it (create an instance of it in the scene).

Transform t = Instantiate(prefab);

From there you can modify the transform to position, rotate, and scale it as well as whatever else you need to do with it.

4 Likes

Thanks a lot for your answers !

Sorry for being a necromancer, but this thread comes up early in search engines.

For others finding this thread, the above is simply not true. Here’s Unity’s guide “Best Practices for the Resources System”. The first sentence is “Don’t use it.”

1 Like