(SOLVED) Unable to load Sprites from Resources folder

Hi,

I’ve searched around and I can’t figure out what I’m doing wrong with the following code to load a sprite from the resources folder, and update a UI image.

The scenario is that I’m trying to change an image displayed on the UI based on a changing value.

I have the following fields set in my class:

private Sprite closedEyesSprite
private Sprite openEyesSprite 
private Sprite angryFaceSprite

And I’ve tried to set the values of them in the following ways

    private Sprite closedEyesSprite = Resources.Load("Sprites/Icons/Eyes/Eye-Off-Outline-Icon") as Sprite;

    private Sprite openEyesSprite = Resources.Load("Sprites/Icons/Eyes/Eye-Outline-Icon") as Sprite;

    private Sprite angryFaceSprite = Resources.Load("Sprites/Icons/Eyes/Eyes-Pressed-Smiley-Outline") as Sprite;

And…

    private Sprite closedEyesSprite = Resources.Load<Sprite>("Sprites/Icons/Eyes/Eye-Off-Outline-Icon");

    private Sprite openEyesSprite = Resources.Load<Sprite>("Sprites/Icons/Eyes/Eye-Outline-Icon");

    private Sprite angryFaceSprite = Resources.Load<Sprite>("Sprites/Icons/Eyes/Eyes-Pressed-Smiley-Outline");

In my file structure, the sprites are stored in the following location: Assets/Resources/Sprites/Icons/Eyes/

The files themselves are PNG files, which in the editor are set as “Sprite (2D and UI)”. If I manually set the Source Image value of the Image resource attached to the GameObject I want to be able to change by dragging one of the files into the Source Image box, then it renders correctly.

However my issue is that at Runtime, the variables that should have the Sprites loaded from the Resource folder are always Null.

Is there something SUPER simple and stupid that I’m doing wrong?

Can you post the full script just to be sure?

You should not be using Resources.Load in a field intializer.

Does it look like this?

private Sprite openEyesSprite;

void Start() {
   openEyesSprite = Resources.Load("Sprites/Icons/Eyes/Eye-Outline-Icon") as Sprite;
 }

Or something else?

1 Like
public class Control : MonoBehaviour
{

private Sprite closedEyesSprite;

private Sprite openEyesSprite;

private Sprite angryFaceSprite;

private void Start()
{
this.panelOnDisplay = false;

closedEyesSprite = Resources.Load("Sprites/Icons/Eyes/Eye-Off-Outline-Icon") as Sprite;
openEyesSprite = Resources.Load("Sprites/Icons/Eyes/Eye-Outline-Icon") as Sprite;
angryFaceSprite = Resources.Load("Sprites/Icons/Eyes/Eyes-Pressed-Smiley-Outline") as Sprite;

this.anim = this.infoPanel.GetComponent<Animator>();
this.anim.enabled = false;

EventManager.Instance.AddListener<SensorClickEvent>(this.UpdatePanel);
EventManager.Instance.AddListener<TemperatureEvent>(this.UpdateTemperatureDisplay);
EventManager.Instance.AddListener<TamperEvent>(this.UpdateTamperEventDisplay);
EventManager.Instance.AddListener<UltravioletEvent>(this.UpdateUltravioletDisplay);
EventManager.Instance.AddListener<HumidityEvent>(this.UpdateHumidityDisplay);
EventManager.Instance.AddListener<LuminescenseEvent>(this.UpdateLuminescenceDisplay);
}

private void UpdateIcon(string eventState)
{
Image motionStatusImage = this.gameObject.transform.Find("StatsDisplay/Motion Indicator/Motion Status Image").gameObject
.GetComponent<Image>();
switch (eventState)
{
case "0":
motionStatusImage.sprite = closedEyesSprite;
motionStatusImage.color = Color.blue;

break;
case "3":
motionStatusImage.sprite = this.angryFaceSprite;
motionStatusImage.color = Color.red;
break;
case "8":
motionStatusImage.sprite = this.openEyesSprite;
motionStatusImage.color = Color.yellow;
break;
default:
break;
}
}
}

Ok so it was something stupid…
A combination of stupid things actually.

Firstly, I was trying to initialise the field outside of the Start function (dumb)
Secondly, when loading the sprites inside the Start function you need to use

Resources.Load<Sprite>()

and not

Resources.Load() as Sprite;

Sigh…

10 Likes

lol glad you figured that out. I was trying to work this out for your question, and just got it (5 mins after you).
I kept trying with ‘as Sprite’ and it never ever worked. (though it would load as ‘object’, but unusable).
So, I finally thought I’d give the version a try, and sure enough it worked…lol

Oh well :slight_smile: Glad you got it working.

3 Likes

Was having the same problem. This solution is correct. Does anyone know why Resources.Load() works and Resources.Load() as Sprite; doesn’t?

Not a answer I am asking a question please help…
My image file is in Asset > Src > 11.prefab

Sprite sprite = Resources.Load<Sprite>("Src/11");

I am also using this line in my project but it’s not read my Sprite prefab which full name is 11.prefab
I also try different image files like .png and .jpg but it’s not work at all.
Also try

Sprite sprite = Resources.Load<Sprite>("Src");

That’s also not work for me.

Then make a new post, don’t necro-hijack someone else’s post;

When you make your own NEW post, here is how to report your problem productively in the Unity3D forums:

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

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

We know Resources.Load() works fine, so be sure to work through the docs and example code. It has VERY specifically detailed requirements. Be sure you followed them all.

I wrote something likes ( all the combination)


       var sprite = Resources.Load($"Assets/Resources/Sprite/Components/Button/Button01_Red.Png") as Sprite;
       var sprite2= Resources.Load($"Assets/Resources/Sprite/Components/Button/Button01_Red") as Sprite;
       var sprite3 = Resources.Load<Sprite>($"Assets/Resources/Sprite/Components/Button/Button01_Red");
       var sprite4 = Resources.Load<Sprite>($"Resources/Sprite/Components/Button/Button01_Red");
       var sprite5 = Resources.Load<Sprite>($"Sprite/Components/Button/Button01_Red");
       var sprite6 = Resources.Load<Sprite>($"Components/Button/Button01_Red");
       var sprite7 = Resources.Load<Sprite>($"Assets/Resources/Sprite/Components/Button/Button01_Red.Png");
       var sprite8 = Resources.Load<Sprite>($"Resources/Sprite/Components/Button/Button01_Red.Png");
       var sprite9 = Resources.Load<Sprite>($"Sprite/Components/Button/Button01_Red.Png");

and only number 5 works for me
var sprite5 = Resources.Load($“Sprite/Components/Button/Button01_Red”);
mean full address without “Assets/Resources” and without “.Png”

so the final code was

var sprite = Resources.Load<Sprite>($"Sprite/Components/Button/Button01_Red");

Since this keeps getting necroed: the reason why Resources.Load("Path_To_Texture") as Sprite; returns null is that if you don’t give a type to Resources.Load, it’s going to load the main asset at the path, which is a Texture2D, not a Sprite - the Sprite is a sub-asset of the Texture. If you use a typed Resources.Load, you get the first asset found at that path with that type.

Since Resources.Load returns the Texture2D, and as returns null if you try to cast to a different type than the one expected, you get null.

This is one of the reasons why I hate that a lot of tutorials use foo as X as the common way of casting instead of (X) foo, since if OP had been doing this:

(Sprite) Resources.Load();

They would have gotten an exception saying “you’re trying to cast a Texture2D to a Sprite”, and understood what was going on.

2 Likes

YES… This ^ ^ ^ I even have my own blurb for this common mistake, unfortunately propagated by too many youtube tutorial-makers, and even propagated by Unity themselves in some of their example code.

Always use Resources.Load<T>(), never use Resources.Load() as T

And the same goes for Resources.LoadAll<T>()

Generally you want to avoid writing code that is simply waiting to mysteriously fail simply by the future appearance of an identically-named file of a different type.

ALSO: Always put your resources in type-ish subfolders if you use Resources.LoadAll(). Here’s why:

As of this writing, if you use Resources.LoadAll<T>("foldername/");, Unity will load ALL FOUND ASSETS at that path into memory, then check each type and only return to you the ones that meet type T. But Unity will have already loaded all the assets it finds in that path, so expect massive performance problems if you don’t use subdirectories, even crashes due to running out of memory if you have a lot of assets there.