I have googled plenty and only come up with really complicated methods of doing it. I also found the function OnMouseDown() but I haven't been able to make it work.
At the moment the sprite activates when you tap anywhere on screen.
EDIT - Yes it has a 2d Box collider
My code below:
using UnityEngine;
using System.Collections;
public class mute : MonoBehaviour
{
public bool isMuted = false;
public Sprite mute1, mute2;
private SpriteRenderer spriteRenderer;
private void Start ()
{
spriteRenderer = GetComponent<SpriteRenderer>();
if (spriteRenderer.sprite == null)
spriteRenderer.sprite = mute1;
}
private void Update ()
{
if (Input.GetKeyDown (KeyCode.Mouse0))
{
if (!isMuted)
{
AudioListener.pause = true;
AudioListener.volume = 0;
isMuted = true;
ChangeSprite();
}
else
{
AudioListener.pause = false;
AudioListener.volume = 1;
isMuted = false;
ChangeSprite();
}
}
}
private void ChangeSprite() => spriteRenderer.sprite =
spriteRenderer.sprite == mute1 ? mute2 : mute1; }