I have started working on a new game and I want to make letters from a sprite sheet that change given the letter or number I give it. I created a numbers only sprite sheet in Photoshop just to test it and imported it into Unity as a Sprite. Then in a script I did this:
public string currLetter;
public string lettersName;
Sprite[] lettersAll;
void Awake () {
lettersAll = Resources.LoadAll<Sprite> ("Textures/" + lettersName);
}
void Update () {
switch (currLetter) {
case "0":
gameObject.GetComponent<SpriteRenderer> ().sprite = lettersAll[0];
break;
case "0":
gameObject.GetComponent<SpriteRenderer> ().sprite = lettersAll[1];
break;
}
}
I am getting an error in the switch statement where if it’s for example number 1, it says that the array index is out of range and when I set the lettersAll to public it had 0 sprites. What am I doing wrong ? I have been trying to fix this all day but nothing works :/
Update:
Image of Sprite Sheet:
2
Answers
Is your spritesheet under “Resources” folder?
For your case the sprite file should be under “Resources/Textures/”
Updated:
I was able to duplicate the issue and solved it by making sure the following things are correct:
will not be loaded because they have no sprites inside, even though
the mode is set to multiple. Spritesheet E will be loaded correctly
Sprites of sprite sheet can’t be accessed like this, as the sprite sheet itself is a texture.
I would recommend don’t place your images in Resources, as it loads immediately at the start of the game, instead of the load at need.
Try UI Sprite Manager which follows this