Making clickable parts to Rhythm game

I’m making a rhythm game, and I am trying to change the buttons from keybindings to work when they are clicked on, currently, when I click anywhere on the screen it triggers them, and I don’t know how to make it so only the one clicked on works.

using System.Collections;
using UnityEngine;

public class Activator : MonoBehaviour {

SpriteRenderer sr;
public KeyCode key;
bool active = false;
GameObject note;
Color old;
public bool createMode;
public GameObject n;

void Awake() {
sr = GetComponent();
}

void Start(){
old = sr.color;
}

void Update() {

if (createMode && Input.GetMouseButtonDown(0)) {
if (Input.GetMouseButtonDown(0))
Instantiate(n, transform.position, Quaternion.identity);
} else {

if (Input.GetMouseButtonDown(0))
{

StartCoroutine(TaskOnClick());
}
if (Input.GetKeyDown(KeyCode.Mouse0) && active)
{
Destroy(note);
AddScore();
active = false;
}
}
}

void OnTriggerEnter2D(Collider2D col){
active=true;
if(col.gameObject.tag==“Note”)
note=col.gameObject;
}

void OnTriggerExit2D(Collider2D col){
active=false;
}

void AddScore() {
PlayerPrefs.SetInt(“Score”, PlayerPrefs.GetInt(“Score”) + 100);
}

IEnumerator TaskOnClick() {

sr.color = new Color(0, 0, 0);
yield return new WaitForSeconds(0.2f);
sr.color = old;
}

}

Don’t use Input.GetMouseButtonDown();. This just checks if the user has pressed an input anywhere at any time.
Instead, use the OnMouseDown function. This function will fire when the object the script is attached to is clicked on.

1 Like