0
\$\begingroup\$
    if(Input.GetKey("y")){
        gameObject.GetComponent<SpriteRenderer>().sprite = Characters[0];

    }
    if(Input.GetKey("u")){
        gameObject.GetComponent<SpriteRenderer>().sprite = Characters[1];
    }
    if(Input.GetKey("i")){
        gameObject.GetComponent<SpriteRenderer>().sprite = Characters[2];
    }

This is my current script, which changes character at keypress Y, U, and I. However as I am developing for touch, I want them to change when a box at the screen is pressed (Left one):

enter image description here

How can I proceed with this? Can I set the sprites to numbers, and let's say at button press, it adds +1, and that changes the sprite? And if so, how does the last one work to get it back at 0? Appriciate all answers. :)

\$\endgroup\$
1
\$\begingroup\$

Something like this:

selected_character++;
if (selected_character >= num_characters)
{
    selected_character = 0;
}

and you can select the sprite by

gameObject.GetComponent<SpriteRenderer>().sprite = Characters[selected_character];

You also have to declare a new class member to make the code above work:

int selected_character = 0;
\$\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.