- Home /
Why does the game detect every click?
So i made a script that detects clicks on gameobjects. The only problem is that anywhere i click it counts it as a click on both of the gameobjects (both of the gameobjects have different scripts)-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StoveScript : MonoBehaviour {
public GameObject playerGameObject;
void Update()
{
OnMouseDown ();
}
void OnMouseDown()
{
PlayerDef playerScript = playerGameObject.GetComponent<PlayerDef> ();
if (Input.GetMouseButtonDown (0))
{
if (playerScript.currentHunger < playerScript.maxHunger)
{
playerScript.currentHunger += 10;
Debug.Log (playerScript.currentHunger + " hunger");
}
}
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BedScript : MonoBehaviour {
public GameObject playerGameObject;
public void Update()
{
OnMouseDown ();
}
void OnMouseDown()
{
PlayerDef playerScript = playerGameObject.GetComponent<PlayerDef> ();
if (Input.GetMouseButtonDown(0))
{
if (playerScript.currentTiredness < playerScript.maxTiredness)
{
playerScript.currentTiredness += 10;
Debug.Log (playerScript.currentTiredness + " Tiredness");
}
}
}
}
I've used this code before (i have to redo the whole game again because i lost it) and it worked.
I entered unity again and everything is broken in my game. The button animation is broken as well.. everything.
Create an empty scene, add 2 cubes and attach one script to one cube, and another script to the other cube. Test to see if On$$anonymous$$ouseDown() is being called correctly. if it is being called correctly, then you have set up something wrong in your scene (did you add both scripts to the same object?). Otherwise, send a bug report to Unity.
EDIT: Remove "On$$anonymous$$ouseDown" from update. This is called automatically from Unity when you click on a collider. This is your problem here.
Also remove the "Input.Get$$anonymous$$ouseButtonDown" check. Its unnecessary, as that mouse button must have been pressed for that function to be called in the first place.
Answer by kebupapa_unity · Apr 16, 2018 at 06:08 AM
Thank you everyone for helping, but Unity decided to crash and my game doesnt respond to anything so I can't even test it that way, but thank you again for being an amazing and helpful community!
Answer by MarioSantoso · Apr 15, 2018 at 11:34 PM
Several thing to try
Take out the "OnMouseDown();" from update;
Make sure each object has their own collider (do not use a single collider for all the objects)
Answer by Rugbug_Redfern · Apr 15, 2018 at 11:38 PM
Input.GetMouseButtonDown is set to true whenever the mouse is clicked. It doesn't matter if the mouse is on the gameObject, Input.GetMouseButtonDown is called whenever you click the mouse.
If you want to detect when the mouse is clicked on the gameObject, do using System.Collections; using System.Collections.Generic; using UnityEngine;
public class StoveScript : MonoBehaviour {
public GameObject playerGameObject;
void Update()
{
}
void OnMouseDown()
{
PlayerDef playerScript = playerGameObject.GetComponent<PlayerDef> ();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if(hit.transform.gameObject == playerGameObject) {
if (playerScript.currentHunger < playerScript.maxHunger)
{
playerScript.currentHunger += 10;
Debug.Log (playerScript.currentHunger + " hunger");
}
}
}
}
}
Make sure OnMouseDown() isn't in update, that method will be called automatically by Unity whenever the mouse is clicked.