[C#]Adding a Sprite from a Resources.Load("Path") : How does it work ?

Hello uniters - not sure about this one …

First thing first, here is the thing I got problem with :

if (SpriteSelector == null) { // if public Sprite variable is null
    	Object textureCheck = Resources.Load (defaultTexturePath);
    	if (textureCheck != null) {
    		SpriteSelector = textureCheck as Sprite; // here's the core problem
    	}
}

There’s no more Log Error with the code, but SpriteSelector is still null after this.

I’m loading this within “void Start()”, tried multiple thing I’ve found on the forum, but still can’t add a default Sprite if none is selected.

The path within “defaultTexturePath” is valid, tried it with “Debug.Log()” by searching the number of files within the Sprite folder : Assets/Resources/2D/Textures. (the string for this variable is “2D/Textures/Diamond”)

Diamond.png is within Textures folder and there is no fault within the string or the path.

The problem here is that I can’t access the sprite with “Resources.Load()”, I can load the Object class from it, but can’t obtain its sprite ; Diamond.png asset as a Texture Type of Sprite within the editor when selected.

Does anyone as the solution for this problem ?

private Sprite mySprite = null;
private string path = “2D/Textures/Diamond”;

void Start()
{
  mySprite = Resources.Load<Sprite>(path);
}

This should work.

“<” Sprite “>” was the answer… I’m feeling dumb… Ahahaha !

Just for the curious :

if (SpriteSelector == null) { 
Sprite textureCheck = Resources.Load<Sprite> (defaultTexturePath);
if (textureCheck != null) { 
SpriteSelector = textureCheck; 
} }

@antx Thank you, dear Sir. :wink:

i would load it async

IEnumerator loadSprite(string path)
    {
       UnityEngine.Object async = Resources.LoadAsync<Sprite>(path);
       yield return async;
       SpriteSelector = async as Sprite;
   }