1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jun 18 '16 at 6:33
  • \$\begingroup\$ @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. \$\endgroup\$ – Demandooda Jun 18 '16 at 14:10
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Jun 18 '16 at 14:17
  • \$\begingroup\$ @DMGregory I think I have cleared it up. \$\endgroup\$ – Demandooda Jun 18 '16 at 14:31
0
\$\begingroup\$

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:

Example of how to call this function from a button press.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.