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. Dismiss Notice

Resolved Resources.Load is not working correctly

Discussion in 'Scripting' started by Kool120, Jan 7, 2021.

  1. Kool120

    Kool120

    Joined:
    Jan 16, 2019
    Posts:
    6
    Hey there!

    So, I'm trying to load a Sprite and add in a Dictionary<int, Sprite> using the Resources.Load().

    The problem is it's returning null, as if he it was unreachable. But if I Debug.Log() the same line, it gets me exactly what I want.


    Code (CSharp):
    1. Debug.Log(Resources.Load("Images/A_Key")); //Works Fine
    2.  
    3. keys.Add(0, Resources.Load("Images/A_Key") as Sprite); //Returns null
    What exactly am I doing wrong?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    Always best to use the templated version.

    Code (csharp):
    1. var sprite = Resources.Load<Sprite>("Images/A_Key");
    2. keys.Add(0, sprite);
    The big-picture reason (which might not apply in your case!): if you do
    Resources.Load("Foo")
    , Unity will load the first thing that matches "Foo" and if that is a model, and you're expecting a texture, you're gonna always fail.
     
    jiyongman likes this.
  3. Kool120

    Kool120

    Joined:
    Jan 16, 2019
    Posts:
    6
    Hey!

    Sorry for the late response...

    I just tried it and it didn't work.

    I think the issue isn't about the specification of the type of file loaded. I think he doesn't reach it at all when Loaded directly into a Dictionary.

    But even declaring it before adding it to the dictionary, following what you pointed out, still doesn't work. I still get the "Object reference not set to an instance of an object"
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,832
    Dictionary.Add() can't return null, because it doesn't return anything at all (its return type is void). What exactly is null? How are you testing?

    Are you on the lookout for fake nulls? Unity overloads operator == to make destroyed objects and missing references compare as equal to null even though they're actually not. I'm not sure if Resources.Load returns fake nulls, but if it does, I could believe that you can still Debug.Log the name of a fake-null object but not actually use it as an asset.

    If
    Resources.Load("Images/A_Key")
    is non-null but
    Resources.Load("Images/A_Key") as Sprite
    is null, then the thing you loaded wasn't a sprite (and your problem has nothing to do with the Dictionary).

    If
    Resources.Load("Images/A_Key") as Sprite
    is non-null when you use it directly, but the value that you read out of your Dictionary is null at some later time, then your problem has nothing to do with Resources.Load. Either some other code overwrote that entry in your Dictionary, or Unity unloaded the resource in the intervening time (do you ever tell Unity to unload unused resources?)
     
  5. Kool120

    Kool120

    Joined:
    Jan 16, 2019
    Posts:
    6
    Hi!

    Sorry for the late response...

    Not sure how to answer to that...

    I'm wrong to say that it "returns null". It gives me an error "Object reference not set to an instance of an object" in the line where I try to Load the Sprite and add it to the Dictionary.

    When I Debug.Log the same Resources.Load() it prints "A_Key (UnityEngine.Texture2D). But what I do have inside that folder is an actual sprite.

    What I believe I do, and I might be wrong, with the "Resources.Load("Images/A_Key") as Sprite" is getting that file and load it as a Sprite. Is that what's giving me problems?

    Trying to solve this issue with what you pointed out as a base, I did the following
    Code (CSharp):
    1. keys.Add(0, (Sprite) Resources.Load("Images/A_Key") as Sprite);
    and it still tells me "Object reference not set to an instance of an object"
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    It has to be because you didn't initialize the dictionary. Dictionaries can take nulls of nullable objects!

    Remember, the answer is ALWAYS the same for nullref: ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Kool120 likes this.
  7. Kool120

    Kool120

    Joined:
    Jan 16, 2019
    Posts:
    6
    Yap. You were completely right. It was, in fact, the dictionary's lack of initialization...

    Thank you very much for the explanation and the supporting links!

    I'll be sure to put to practice this mindset more often!
     
    Kurt-Dekker likes this.