J
J
JunEternal2022-03-02 14:45:44
Unity
JunEternal, 2022-03-02 14:45:44

How to change the sprite to the second one when pressing the button and vice versa?

Is it possible to do this in one script? I did not find how to change the sprite on the button in both directions without creating a 2nd button or 2 scripts.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GFX Data, 2022-03-02
@ShockWave2048

The code:

public SpriteRenderer sr;
public Sprite[] sprites;

public void OnButton()
{
    // C условием что в SpriteRenderer со старта первый спрайт, и у спрайтов разные Name
    int index = (sr.sprite.name == sprites[0].name) ? 1 : 0;
    sr.Sprite = sprites[index];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question