M
M
Midzakikayo2021-08-25 21:39:08
Unity
Midzakikayo, 2021-08-25 21:39:08

How to make a sprite change in Unity?

All the best! Need help! Started learning Unity and stuck with the first task... There are three buttons (Rock, Scissors, Paper) and there is some invisible GameObject. It is necessary to make it so that when the button is pressed, the object changes its Sprite to the one that lies in a pre-prepared folder. I watched a lot of videos... and I can't figure out how to do it... Please tell me!

public class Button : MonoBehaviour
{
    public Sprite Button_stn, Button_scs, Button_pap, GameObj;
    public void Button_stn_cl()
    {
        GameObj = Resources.Load("1") as Sprite;
    }

    public void Button_scs_cl()
    {
        GameObj = Resources.Load("2") as Sprite;
    }

    public void Button_pap_cl()
    {
        GameObj = Resources.Load("3") as Sprite;
    }

    public void Game_object()
    {
        GetComponent<SpriteRenderer>().sprite = GameObj;
    }

    void Start()
    {
        
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
d1msk1y, 2021-08-27
@d1msk1y

need a link to the sprite renderer and the sprites themselves.
public SpriteRenderer sr;
public Sprite[] sprites //Array of sprites
throw all the sprites into an array and specify the desired sprite in the sprite renderer.
sr.sprite = sprites["Desired sprite"];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question