Hello there! I’m new on Unity 2D, and i’ve already built the main menu of my game. But now i need to make a character customization. I have created more than one hundread sprites. And i’ve made buttons that its function is to change the character’s characteristics. And is now that came my doubt: How can i change the Sprite by just One Object, using path location?
Here is what i’ve already made: A sprite object without sprite image; GUI buttons;
What i need: Sprite change command using path location.
What i tried: gameObject.GetComponent().sprite = Resources.Load(“/Character/Type001”, type of(Sprite)) as Sprite;
Thanks!
I found the error by searching the threads that have similar problem. Thanks to amjadyahya that have post on his thread a solution that fix the issue.
public Sprite[] fruitSprites;
void Awake()
{
// load all frames in fruitsSprites array
fruitSprites = Resources.LoadAll<Sprite>("fruits");
}
void Start ()
{
// create the object
GameObject fruit = new GameObject();
// add a "SpriteRenderer" component to the newly created object
fruit.AddComponent<SpriteRenderer>();
// assign "fruit_9" sprite to it
fruit.GetComponent<SpriteRenderer>().sprite = fruitSprites[9];
// to assign the 5th frame
fruit.GetComponent<SpriteRenderer>().sprite = fruitSprites[5];
}
The problem is that i was trying to load a sprite sheet (multiple), instead of a simple sprite, and putting on a simples Sprite Variable. So i change it to an Array and replace Resources.Load by Resources.LoadAll, and storage the sprite frame in the Array.