I have two gameobjects with scripts attached to them. In first script, I’ve implemented OnMouseDown() so the script reacts when user is clicking that object. And I want second object to react when i’m clicking elsewhere on the screen. I’ve tried Input.GetMouseButtonDown() but that doesn’t seem to work, because in this case second script runs even if i click first object.
You could use a boolean that is set to true or false depending on whether the mouse is on the object. Something like:
var MouseOnObject : boolean = false;
function OnMouseEnter() {
MouseOnObject = true;
}
function OnMouseExit() {
MouseOnObject = false;
}
function Update () {
if(Input.GetButtonDown("Fire1") && !MouseOnObject)
{
//clicked elsewhere on screen
}
}
This should work but give it a try and let me know
this post is quite old but, but still useful i changed the code a bit and for C# it works like a charm
using UnityEngine.EventSystems;
public class DisableGameObjectClickedOut : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
bool MouseOnObject = false;
public void OnPointerEnter(PointerEventData pointerEventData)
{
MouseOnObject = true;
}
public void OnPointerExit(PointerEventData pointerEventData)
{
MouseOnObject = false;
}
void Update()
{
if ((Input.GetMouseButtonDown(0)) && (!MouseOnObject))
{
this.gameObject.SetActive(false);
}
}
}