Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Sprite Changing - Customization Character

Discussion in '2D' started by MESMascoli, Sep 28, 2015.

  1. MESMascoli

    MESMascoli

    Joined:
    Sep 28, 2015
    Posts:
    5
    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<SpriteRenderer>().sprite = Resources.Load("/Character/Type001", type of(Sprite)) as Sprite;
    Thanks!
     
  2. Kuan

    Kuan

    Unity Technologies

    Joined:
    Jul 2, 2014
    Posts:
    87
  3. MESMascoli

    MESMascoli

    Joined:
    Sep 28, 2015
    Posts:
    5
    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.

    Code (CSharp):
    1. public Sprite[] fruitSprites;
    2. void Awake()
    3.     {
    4.         // load all frames in fruitsSprites array
    5.         fruitSprites = Resources.LoadAll<Sprite>("fruits");
    6.     }
    7. void Start ()
    8.     {
    9.         // create the object
    10.         GameObject fruit = new GameObject();
    11.         // add a "SpriteRenderer" component to the newly created object
    12.         fruit.AddComponent<SpriteRenderer>();
    13.         // assign "fruit_9" sprite to it
    14.         fruit.GetComponent<SpriteRenderer>().sprite = fruitSprites[9];
    15.         // to assign the 5th frame
    16.         fruit.GetComponent<SpriteRenderer>().sprite = fruitSprites[5];
    17.     }
    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.

    That works perfectly.!! Thanks to amjadyahya.