Hello! I currently have a script that activates when the player’s mouse hovers over the hitbox of whatever the script is attached to. However, I cannot for the life of me figure out how to use it with onMouseDown and onMouseUp. I have this script:
var _mouseOver = false;
function OnGUI()
{
if(!_mouseOver) {
return;
}
GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
}
function OnMouseOver()
{
_mouseOver = true;
}
function OnMouseExit()
{
_mouseOver = false;
}
I have tried changing OnMouseOver and OnMouseExit, but to no avail. How would I do this?
I think that this is caused by my key script:
#pragma strict
var collected = false;
function OnMouseDown() {
collected = true;
Destroy(gameObject);
}
I’m not sure, I have understood all your need.
But if you want to detect when a mouse is over and click down on an object,
this script is for you :
var _mouseOver = false;
function OnGUI(){
if(_mouseOver) {
GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
}
}
function OnMouseOver() {
_mouseOver = true;
}
function OnMouseExit() {
_mouseOver = false;
}
function OnMouseDown () {
if(_mouseOver){
Debug.Log("Over && Down");
}
}
Update:
Something like this ?
var _mouseDown = false;
function OnGUI() {
if(_mouseDown) {
GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
}
}
function OnMouseDown () {
_mouseDown = true;
}
function OnMouseUp () {
_mouseDown = false;
}
I am thinkng that this is what you are looking for:
var _mouseOver = false;
var _mouseClick = false;
function OnGUI(){
if(_mouseClick) {
GUI.Label (Rect (10, 10, 100, 20), "Clicked a key!");
}
}
function OnMouseOver() {
_mouseOver = true;
}
function OnMouseExit() {
_mouseOver = false;
}
function OnMouseDown () {
if(_mouseOver){
_mouseClick=true;
}
}
function OnMouseUp () {
_mouseClick=false;
}
The script makes the text show when you press your mouse button and while hovering over the location. Then when you release the mouse button, the text goes away. If you want the text to stay after mouse click, simply remove the OnMouseUp function.