Reputation: 129
Yes, I have read all the topics with similar problems. However mine is not solved yet :(
So could you please take a look into it?
Like everyone else, I'm trying to change a sprite of an object via code. The sprite file itself is in "Assets/Resourses" folder. The import settings state the texture type of the imported file is "Sprite(2D and UI)".
I've tried the following methods:
gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>("1");
and
gameObject.GetComponent<SpriteRenderer>().sprite = Resources.Load("1") as Sprite;
and
Sprite s = Resources.Load("1") as Sprite;
gameObject.GetComponent<SpriteRenderer>().sprite = s;
and
Sprite s = Resources.Load<Sprite>("1");
gameObject.GetComponent<SpriteRenderer>().sprite = s;
All of them replace existing object sprite with "None(Sprite)", which I guess, means "null".
Any help would be really appreciated!
Upvotes: 2
Views: 4689
Reputation: 125245
The sprite file itself is in "Assets/Resourses" folder.
That's the problem. The folder must be named Resources not Resourses. It's not Resource too.
It has to be named named Resources. Once you fix the folder name, you can use the snippet below to read the Sprite
.
Sprite sprite = Resources.Load("1", typeof(Sprite)) as Sprite;
if the sprite is set to multiple mode then use this;
Sprite[] sprite = Resources.LoadAll<Sprite>("1") as Sprite[];
You can find other ways to load Sprites here.
Upvotes: 6