- Home /
how do i fix not being able to click properly in my clicker game?
i have recently been trying to make a clicker game but when I click, my counter doesnt go up. I use left click, right click and space button as my clicking buttons. I can only make the counter go up when i click space. I have tried to use just left click and remove space and right click but now the counter just doesnt go up when i click (as i expected)
Here is my code
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
public void OnClick1()
{
if (Input.GetKeyDown(KeyCode.Mouse0))
{
ScoreValue += 1;
}
score.text = "Blocks: " + ScoreValue;
}
public void OnClick2()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
ScoreValue += 1;
}
score.text = "Blocks: " + ScoreValue;
}
public void OnClick3()
{
if (Input.GetKeyDown(KeyCode.Space))
{
ScoreValue += 1;
}
score.text = "Blocks: " + ScoreValue;
}
}
Answer by Marioooo · Aug 11, 2021 at 07:32 PM
Hello!
I can see that you're not using actually update method to check your input... you just made 3 voids that are never used
so i remade your code
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;
}
you still have the OnClick method to call it from outside if you need.
Hope this helps!
Thank you so much. I was trying to figure out why it didnt work for ages!
Your answer
![](https://koobas.hobune.stream/wayback/20220613041532im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Button OnClick() seemingly not triggering on Mac 0 Answers
4.6 UI Hover & Click sound? 1 Answer
Click on Handles 0 Answers
Detecting mouse click on GameObject with any mouse button. 2 Answers
Change sprite images on button click 0 Answers