- Home /
Text popup onMouseDown
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);
}
Answer by termway · Jul 30, 2012 at 03:24 PM
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 want ins$$anonymous$$d of hovering to activate gui, you click.
Hm... the text is not appearing... is it because i destroy the key after I click it?
Yes it delete your object and your script. "Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering." http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html
Answer by TwistedGalaxy · Jul 30, 2012 at 04:01 PM
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.
How do I use this script with my object? Do I just create a new class called Textlabel.js and attach this piece to my object?
Your answer
Follow this Question
Related Questions
Display message when OnMouseDown 2 Answers
OnMouseDown 2 Answers
How to: Click top Object of 2 overlapping objects (2D) 1 Answer
Player Fast Travels 1 Answer
How to disable a part of a script to avoid replayed camera animations ? 0 Answers