Resources.Load is not working correctly

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.

Debug.Log(Resources.Load("Images/A_Key")); //Works Fine

keys.Add(0, Resources.Load("Images/A_Key") as Sprite); //Returns null

What exactly am I doing wrong?

Always best to use the templated version.

var sprite = Resources.Load<Sprite>("Images/A_Key");
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.

1 Like

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”

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?)

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
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”

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://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

1 Like

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!

1 Like