Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Alternative to Resources.Load ?.

Discussion in 'Getting Started' started by CCnockaert, Sep 26, 2018.

  1. CCnockaert

    CCnockaert

    Joined:
    Sep 4, 2018
    Posts:
    28
    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 ?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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.
     
    CCnockaert likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,753
    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.
    Code (csharp):
    1. public Transform prefab;
    When you want to instantiate it (create an instance of it in the scene).
    Code (csharp):
    1. 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. CCnockaert

    CCnockaert

    Joined:
    Sep 4, 2018
    Posts:
    28
    Thanks a lot for your answers !
     
  5. JustASloth13

    JustASloth13

    Joined:
    Sep 6, 2019
    Posts:
    2
    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."