- Home /
Question about mouse events with box collider triggers
Hello,
Is it possible for a "OnMouseUp()" to be only used once?
Here is an explanation of this question. Picture a scenario where you are flowing through a menu system (Main menu, World Select, Level Select). Now the buttons for each screen are;
(Main Menu = Play Game) (World Select = World 1 - 4) (level Select = Level 1 - 5, Back Button)
Now my problem here is that, i can function the whole menu system to where i can navigate all the way through to being able to click the back button to go back to the world select. Only problem is, when i attempt to click World 1 again, it does not want to do anything.
I have the button to be a 3D text, with a box collider around it. This is a trigger so i can click the button. Below is my sample code;
var tm : TextMesh;
var tmtwo : TextMesh; var tmthree : TextMesh; var tmfour : TextMesh;
static var pressed = false;
function OnMouseEnter() { if(pressed == false) { renderer.material.color = Color.red;
} else if(pressed == true) { renderer.material.color = Color.blue;
} //renderer.material.color = Color.green; }
function OnMouseExit() { renderer.material.color = Color.white; }
function Update () {
if(worldone.drawit == true) { tm.GetComponent(TextMesh).text = "World One"; tmtwo.GetComponent(TextMesh).text = "World Two"; tmthree.GetComponent(TextMesh).text = "World Three"; tmfour.GetComponent(TextMesh).text = "World Four";
tm.renderer.enabled = true;
tmtwo.renderer.enabled = true;
tmthree.renderer.enabled = true;
tmfour.renderer.enabled = true;
} else if(worldone.drawit == false) { tm.renderer.enabled = false; tmtwo.renderer.enabled = false; tmthree.renderer.enabled = false; tmfour.renderer.enabled = false; } / if(backbutton.pressed == true) { pressed = false;
}/
}
function OnMouseUp() { pressed = true; renderer.material.color = Color.green;
if(backbutton.pressed == true)
{
tm.renderer.enabled = true;
tmtwo.renderer.enabled = true;
tmthree.renderer.enabled = true;
tmfour.renderer.enabled = true;
}
}
To explain more about my code; tm is represented to the 3D Text i am trying to click. It has a box collider on it and is ticked as a trigger (so are, tmtwo, three and four). Pressed is the boolean i use to state if i have clicked the button or not.
The OnMouseEnter; For testing purposes i am changing the color to see what state the boolean is in.
The Update; Worldone is another JS file where when an action not explained in this question(it is irrelevant) is hitting a collider to make the texts "World one - four" appear on screen. As shown by the true statement.
When that said irrelevant action is not in that collider, the text is not to be shown.
The OnMouseUp; When the button is pressed, it changes the boolean to be true so that the text gets removed and it will move into the level select screen.
The JS file for the "Worldone" and "backbutton" can be shown below.
Backbutton
var tm : TextMesh;
static var pressed = false;
function OnMouseEnter() { renderer.material.color = Color.green; }
function OnMouseExit() { renderer.material.color = Color.white; }
function OnMouseUp() { pressed = true; }
function Update() { if(worldselect.pressed == true) { tm.GetComponent(TextMesh).text = "Back"; } if(pressed == true) { tm.renderer.enabled = false; } }
Worldone
var triggerhit = false; static var drawit = false;
function OnTriggerEnter() { triggerhit = true; }
function OnTriggerExit() { triggerhit = false; } function OnGUI() { if(triggerhit == true) { drawit = true; } if(triggerhit == false) { drawit = false; } }
Hopefully i have explained the problem well, if not i will comment more
This problem maybe the way i have structured my code or my logic is wrong, either way i have spent a few hours today on this problem and i need another pair of eyes to see it :)
Thank you Matthew Dennis
i'm not sure why the code clamp didnt work for most of this.