- Home /
how do i make it so i can only click the block in my clicker game?
so ive been trying to make a clicker game and i only want to be able to click a certain area (which at the moment is just a button in the shape of a square). im not 100 percent sure which code i should send but this is the only one i have that isnt an upgrade so im assuming its what i need to send
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BlockCounter : MonoBehaviour
{
public static int ScoreValue = 0;
Text score;
// Start is called before the first frame update
void Start()
{
score = GetComponent<Text>();
}
// Update is called once per frame
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0) || Input.GetKeyDown(KeyCode.Mouse1) || Input.GetKeyDown(KeyCode.Space))
{
OnClick();
}
}
public void OnClick()
{
ScoreValue += 1;
score.text = "Blocks: " + ScoreValue;
}
}
quick edit: i do have a UI button and the script is only attached to the text of that button ( its on the text because it needed a text block to change the text elements of the script)
Answer by AaronBacon · Aug 17, 2021 at 06:00 PM
I think you may Misunderstand how Buttons work. If you select the Button game Object and scroll down to the bottom, you can assign functions that should be run "On Click":
⠀
⠀
So you can directly drag a gameObject in there, and should be able to select any script and then any PUBLIC function that's in the script. This means that if you assign it to run that OnClick() function you made, and then remove the code in Update, then only when you click on the button will that function run.
hey there, thank you for the reply. I dragged the gameobject and selected the onclick void, but im a bit confused as to what you meanby remove the code in update. Do you mean to just delete the code in update because that wouldn't make sense as the void wouldnt exist so the button wouldnt be able to have that void assigned? im sorry if im just being silly
hey there again, I realised I was being very silly. Thank you for the answer, my issue is fixed now :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613041653im_/https://answers.unity.com/themes/thub/images/avi.jpg)