# How to switch between sprites in Unity

So I made an empty game object in inspector, called it "squareTest", and I clicked and dragged 3 sprites in. One is the square being black, one is the square being red, and one is the square being green. What I want to do is this:

In the code, it starts by default as the black sprite. Whenever 'x' happens, the sprite changes to the red square. And whenever 'y' happens, it changes to the green square. BTW, x and y could mean anything. I just want some logic to it, as in

if (password == "Open sesame") {
//Change square color
}


My game programming experience is from Pygame, and this problem could have been solved like this (using a sprite sheet that goes from left to right)

if Condition:
square_spritesheet.current_frame = 5

screen.blit(square_spritesheet.texture, (x, y),
(self.current_frame*self.frame_width, 0, self.frame_width, self.frame_height))
#screen.blit goes (image, (x, y),
#(start chopping_X, start chopping_Y, end chopping_X, end chopping_Y)


Unfortunately, I do not have a clue how this could be accomplished in Unity. I do not know if this is possible with an animator controller. I just want to know if this is possible, and how it could be achieved, with or without an animator. Thank you very much!

The different colored squares are different textures, and I do not need transitions between states.

• Is it necessary to do this with an animator? You could simply have your controlling script toggle the visibility or colour of your sprites directly, cutting out the middle step and any animation wrangling. – DMGregory Jun 18 '16 at 6:33
• @DMGregory I was thinking of having a ui option to change the square color. If that does not need an animator, great! But I do not know how to work with multiple sprites in the same gameObject and not have them animate. – Demandooda Jun 18 '16 at 14:10
• Try describing the actual problem you want to solve, rather than the means of solving it. Depending on what your end goal is, an animator or a control script might be an appropriate solution. Be sure to fill in details like, are you changing the sprite texture, or just the tint colour? Do you need any kind of transition between the states? Etc. – DMGregory Jun 18 '16 at 14:17
• @DMGregory I think I have cleared it up. – Demandooda Jun 18 '16 at 14:31

I'd write a little script like this and attach it to the object displaying the sprite:

[RequireComponent(typeof(SpriteRenderer))]
public class SpriteSwitcher : MonoBehaviour {

// Populate this array in the Unity Inspector window
// to contain the sprites you want to switch between.
public Sprite[] sprites;

SpriteRenderer _spriteRenderer;

// Use this for initialization
void Start () {
// Cache a reference to this component so we don't
// need to look it up every time we want to use it.
_spriteRenderer = GetComponent<SpriteRenderer>();
}

public void SwitchToSprite(int index)
{
// Catch errors where we accidentally passed an index out of range.
if(index < 0 || index >= sprites.Length)
{
Debug.LogError("Attempted to display a sprite outside the valid range: " + index);
return;
}

// Display the selected sprite.
_spriteRenderer.sprite = sprites[index];
}
}


Because the SwitchToSprite() method is public, we can use the Inspector to wire up a UI button to call it when clicked and pass the index of the sprite we want to display: