Search Unity

Change a sprite depending on the type of objects

Discussion in '2D' started by Maklaud, Nov 24, 2013.

  1. Maklaud

    Maklaud

    Joined:
    Apr 2, 2013
    Posts:
    551
    Hi All!

    I have a task - in my game I generate items. In the previous Unity version I used ONE prefab (I used Instantiate) and SEVERAL textures, and I changed textures of the game objects depending on their type. For example, I have enum with values (types) Corn, Pumpkin, Tomato, 3 textures for them and I set a Tomato texture for the Tomato enum value etc.

    But in Unity 4.3 we can make animations or divide textures (atlases) into several sprites. I think I still need that enum, but in this case I don't need to change textures in the game object material, I think I should operate with sprites, but I don't know how to implement it better.

    Thanks for advice!
     
  2. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    Get familiar with the sprite asset and spriteRenderer component, then use the enum to index an array of Sprites.

    spriteRenderer.Sprite = arrayOfSpriteAssets[Type.Corn];
     
  3. Spinnernicholas

    Spinnernicholas

    Joined:
    Jan 24, 2013
    Posts:
    125
    This should work with a sprite atlas or individual sprite textures.