Change sprite image when button is clicked on unity

0

I need a button to click on the image (a shrub) to change (for a cow in front of the shrub).

public class mudaSprite : MonoBehaviour {

public InputField dgt;
public Text txt;
public Sprite arbusto;
SpriteRenderer spriteRenderer = GetComponent<SpriteRenderer>();


public void setget(){

    if (dgt.text.ToLower().Equals("cinza")) {
        if (spriteRenderer.sprite == null) {
            spriteRenderer.sprite = Resources.Load<Sprite>("vacarbustoC");
        }
        ChangeTheDamnSprite ();

        txt.text = "";
    } 
    else {
        txt.text="Tente Novamente!";
    }
}

void ChangeTheDamnSprite(){
    if (spriteRenderer.sprite == arbusto) {
        spriteRenderer.sprite = Resources.Load<Sprite>("vacarbustoC");
    } else {
        spriteRenderer.sprite = arbusto;
    }
}

Pleaaase !!

    
asked by anonymous 06.11.2017 / 20:20

1 answer

1

I've got it!

I put the shrubs off the canvas, like GameObject. So I put the above code associated with the GameObject and not the sprites.

    
10.11.2017 / 14:37