Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resources.Load not working?!!

Discussion in 'Scripting' started by Ted-Chirvasiu, Jul 5, 2011.

  1. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Hello!I have no ideea why this does not work :

    var HM : Texture2D;

    function Update(){

    HM = Resources.Load("Tex",Texture2D);

    }

    --Tex is a jpeg image

    Please help me,i am pulling my hair out! :eek:

    What i wanna do is to assign the image Tex to var HM at runtime.
     
    blox5000 likes this.
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    the jpeg should be in the Resources folder for it to work.
     
    Bunny83 and blox5000 like this.
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html

    you do not need the comma and Texture2D I don't think. I think more likely, you aren't storing your asset in a Resources folder. Make a folder called Resources and put it in there. It only works with that folder.

    And um, please don't put it in your Update loop or it will repeatedly try and load it forever, and probably freeze and die.
     
    Nistroy and Artur2024 like this.
  4. Ted-Chirvasiu

    Ted-Chirvasiu

    Joined:
    Sep 7, 2010
    Posts:
    381
    Thanks,it worked!
     
  5. Jakelopolis

    Jakelopolis

    Joined:
    Dec 2, 2011
    Posts:
    1
    Hi,

    I tried this and it is still not working for me. I have the textures in the assets folder and this is the code that I am using.

    // c# code ///////////////////////////////////
    Texture m_Down;
    //Texture m_Up;
    // Use this for initialization
    void Start ()
    {
    //m_Up = Resources.Load("Play", typeof(Texture));
    m_Down = (Texture)Resources.Load("PressPlay");
    }

    // Update is called once per frame
    void Update ()
    {
    if(Input.GetMouseButtonUp(0))
    {
    if(this.guiTexture.HitTest(Input.mousePosition))
    {
    this.guiTexture.texture = m_Down;
    }
    }
    }
    ///////////////////////////////////////////
    Resources.Load always returns null.
     
  6. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    resources.load will not load from the assets folder only from Assets/Resources
    This folder is completely present in the build and thats a core requirement to load from code (for any other folder this is not the case, the stuff there is only included when assigned to game objects in scenes)
     
    BomboBombom likes this.
  7. AnJo888

    AnJo888

    Joined:
    May 24, 2017
    Posts:
    1
    Thank you. Now... that's not clear in the documentation. In fact, it misleads us to believe that if we create some folders inside Resources we could use them to keep our stuff in a more organized way...
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,150
    It's not misleading. You can create subfolders under Resources. So, Assets/Resources/Player is perfectly valid, however if you want to target the stuff in there you have to do Resource.Load("Player/filename") should work(may need a slash before player also, but can't recall that part off the top of my head). What you can't do is create Assets/Player and then use Resource.Load to get the stuff out of that player folder.
     
    Last edited: May 24, 2017
    Bunny83 and Kiwasi like this.
  9. EdVanGennip

    EdVanGennip

    Joined:
    Apr 3, 2018
    Posts:
    5
    Hello. I am new to Unity. I am trying to load a png file as a Sprite resource. I have read a hundred posts on Resources.Load failing and tried many things. Don't know what is wrong. Here is my story.

    I am testing this by pressing Play inside the Unity UI version '2018.1.1f1 personal' I am running on OSX Sierra 10.13.5.

    help1 is really help1.png stored in the Assets/Resources folder. When I moved it there Unity created a help1.png.meta file automatically.

    35 Sprite s = Resources.Load<Sprite>("help1");
    36 if (s != null)
    37 Debug.Log("loaded help " + s.ToString());
    38 else
    39 Debug.Log("resource load failed");

    when my code executes I get
    resource load failed
    UnityEngine.Debug:Log(Object)
    HelpSwipeHandler:Start() (at Assets/Scripts/HelpSwipeHandler.cs:39)

    The help1.png name is all lowercase. It is 1920x1080 in size, 72 pixels per inch, RGB color. It was created in Keynote and exported as a PNG image.

    Any ideas? Any way to debug and get more info from what Resources.Load is doing?

    Thanks.
     
  10. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,454
  11. EdVanGennip

    EdVanGennip

    Joined:
    Apr 3, 2018
    Posts:
    5
    Peter77 likes this.
  12. hshahbodaghkhan

    hshahbodaghkhan

    Joined:
    Aug 24, 2018
    Posts:
    11
    Hello everyone.
    I have the same problem with Resources.Load
    1- I have the Resources folder.
    2- the texture type of all assets in Resources folder is Sprite (2D and UI)

    here is my code:


    Code (CSharp):
    1.  private SpriteRenderer LaunchButtonSprite;
    2.  
    3.     void Start()
    4.     {
    5.         LaunchButtonSprite = gameObject.GetComponent<SpriteRenderer>();
    6.         LaunchButtonSprite.sprite = Resources.Load("LaunchButtonWith7ShotSlot_3Full") as Sprite;
    7.     }
    The
    Code (CSharp):
    1. LaunchButtonSprite
    has another sprite before running. But when I run the game the sprite turns to "None".
     
  13. hshahbodaghkhan

    hshahbodaghkhan

    Joined:
    Aug 24, 2018
    Posts:
    11
    I found the solution.
    Just changing the
    Code (CSharp):
    1. Resources.Load("LaunchButtonWith7ShotSlot_3Full") as Sprite
    to
    Code (CSharp):
    1. Resources.Load<Sprite>("LaunchButtonWith7ShotSlot_3Full") as Sprite
     
  14. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,454
    Please try:
    Resources.Load<Sprite>("LaunchButtonWith7ShotSlot_3Full");


    I guess if you don't specify the type, it loads the main asset, which would be a Texture2D instead.
     
  15. mrVentures

    mrVentures

    Joined:
    Nov 11, 2018
    Posts:
    9
    My issue was I included the extension ".png" in the path. It requires a path with no extension specified.
     
  16. salmankhan_unity

    salmankhan_unity

    Joined:
    May 31, 2023
    Posts:
    1
    Resources.load only load from Resources folder, make a folder name Resources
     

    Attached Files:

    • ddd.PNG
      ddd.PNG
      File size:
      12.1 KB
      Views:
      67