C# Help: Change sprite @ runtime

Hey everyone,
hoping for some quick help here, why doesn’t my sprite load in?

Doesn’t work:

asdf = Resources.Load ("Sprites/mySprite") as Sprite;

Works fine:

thisMat = Resources.Load ("Materials/Cell") as Material;

I think you must have the resource path mistyped, maybe capitalization or something?

It has to also exist under a Resources folder.

This code works fine… just put a sprite into sr before running:

using UnityEngine;
using System.Collections;

public class LoadSprite : MonoBehaviour
{
    public SpriteRenderer sr;

    IEnumerator Start ()
    {
        while(true)
        {
            yield return new WaitForSeconds( 1.0f);
            sr.sprite = Resources.Load<Sprite>( "Materials/smilesprite");
        }
    }
}

See attached image for a snap of my project directory.

2203219--146341--smilesprite.png

Appreciate the response,
it’s something more than the asset being in resources though, I should have posted the full code to avoid the obvious culprits. There are no misspellings, unfortunately. The materials line loads in perfectly fine and if I were to use the same structure for any other resource they all load in fine as well (as i’ve done in the example below). It just seems that it doesn’t work specifically for sprites…which is slightly confusing.

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
    public Material myMat;
    public Sprite mySprite;
    public Object myObj; //Anything can go here and it'll still load it in, EXEPT a sprite...

    void Start(){
        myMat = Resources.Load ("Materials/myMat") as Material;
        mySprite = Resources.Load ("Textures/Sprites/mySprite") as Sprite;
        myObj = Resources.Load ("mytestObj") as Object;
    }
}

2203273--146347--Untitled.png

I see two sprites in your Textures/Sprites folder, one called asdf and one called face.

I don’t see any called mySprite.

Is that it??

yeah my bad, i deleted it before I took the screenshot apparently…
Well…this post is a bust lol.

The overall question is why can’t I load a sprite from the resources folder, regardless of what that screenshot is showing…it should work, why doesn’t it?

Well, it definitely works, as my code above works fine. Give the templated call a try, the one I wrote in the codelet above:

sr.sprite=Resources.Load<Sprite>("Materials/mySprite");

If you use the format you used, then it will find the first resource of ANY type that matches “mySprite,” and if it fails to cast it to Sprite, you will get a null. It will NOT keep searching until it finds the Sprite one.

That’s why the templated version is preferred.

I.e., if you have a material called foo and a sprite called foo, you cannot reliably load them with the load-and-cast-as structure, you MUST use the templated call. That could be what is happening here…

Okay, here is what is actually going on. Try this codelet:

UnityEngine.Objecto = Resources.Load( "Materials/smilesprite");
Debug.Log ( o);

When you run that, you will see in the log that it loaded a Texture2D, NOT a sprite, even though “smilesprite.tga” is marked as a Sprite import.

Even a sprite is actually a Texture first and foremost, and that’s what the resource loader finds first.

If you append “as Sprite” to that, it will fail to cast a Texture2D to a Sprite and hence return null.

Therefore, use the templated version of Resources.Load to get the sprite, because in that case, Resources.Load will not return the Texture2D but rather the sprite.

If you want to load a bunch of sprites (like a cut sheet array), then use the templated version of Resources.LoadAll() to do so and you’ll get a handy-dandy array of sprites.

You can always create a sprite from a texture.

AWESOME, both of these work actually. Thanks a bunch guys!