- Home /
C# MOUSE EVENT TOGGLE
I posted this in the forums when I meant to post this here. When you post in the forums, people like to post links to help files and documentation pages. I've already looked at these both and couldn't come up with an answer based solely on them, hence me posting my question.
Basically, I am trying to create a script that toggles logic based on whether a sprite (egg) is clicked or unclicked. The code I have now fires the OnMouseDown function twice on the first click, and then the OnMouseUp function twice on the first "un-click", and then nothing happens. I want it to be checked on each frame when the egg is clicked, print the string, and on each frame when the egg is released, print the other string.
// Declare variables
private bool isHeld;
private int clicks;
// Use this for initialization
void Start ()
{
clicks = 0;
}
// Update is called once per frame
void Update ()
{
if (isHeld)
{
OnMouseDown();
}else if (isHeld == false && clicks > 1)
{
OnMouseUp();
}
}
// If player clicks egg, it is picked up
void OnMouseDown ()
{
Debug.Log("You're holding the egg!");
clicks ++;
}
void OnMouseUp ()
{
isHeld = false;
Debug.Log("You released the egg!");
}
}
I'm thinking the double function firing is because it listens for the MouseDown and MouseUp once automatically, so that will fire it once, and then in my code I'm telling it to fire again by changing the isHeld boolean. I'm not sure logically how to work around this. Also, it only happens once instead of each time and I don't understand why.
Answer by Statement · Dec 28, 2011 at 04:36 AM
It fires twice since you are calling it inside Update as well. I see 4 events. One that fires when you pick up the egg. One that fire every frame while you are holding the egg. One that fires when you drop the egg. Lastly, one that fire every frame while you are not holding the egg.
You can see which parts of the code this happen by reading the debug logs.
private bool isHeld;
void Update()
{
if (isHeld)
{
Debug.Log("You're holding the egg!");
}
else
{
Debug.Log("You're not holding the egg!");
}
}
void OnMouseDown()
{
Debug.Log("You picked up the egg!");
isHeld = true;
}
void OnMouseUp()
{
Debug.Log("You dropped the egg!");
isHeld = false;
}
You can also create dedicated callbacks for these to make it crystal clear what the code should be doing and when it should be done:
private bool isHeld;
void Update()
{
if (isHeld)
OnEggHeld();
else
OnEggNotHeld();
}
void OnMouseDown()
{
isHeld = true;
OnEggGrabbed();
}
void OnMouseUp()
{
isHeld = false;
OnEggDropped();
}
void OnEggHeld()
{
// Called while the egg is being held,
// put code here that should happen when it is held.
}
void OnEggNotHeld()
{
// Called while the egg is NOT being held,
// put code here that should happen when it isn't held.
}
void OnEggGrabbed()
{
// Called when the egg is grabbed,
// put code here that should happen when it is grabbed.
}
void OnEggDropped()
{
// Called when the egg is dropped,
// put code here that should happen when it drops.
}
This doesn't seem to work. It keeps crashing Unity every time I run this, but doesn't print an error message. I don't understand why my code wasn't working in the first place. $$anonymous$$aybe I don't understand how Update() works. I thought it was like the enter_frame works in Actionscript. I thought Update() ran the logic each frame. So like, if I had this:
void Update()
{
Debug.Log("This is a new frame!");
}
And my project was running at 24 frames/second, every second, "This is a new frame!" should be printed 24 times. However, it is only printed once. Why is this?
LOL, I figured it out. 'Collapse' needed to be toggled off. Jeez that took way too long to figure out. Ha, ha. I suck at program$$anonymous$$g.
Your answer
Follow this Question
Related Questions
OnMouseDown 2 Answers
Check if an image has been clicked 1 Answer
OnMouseDown Range Problem 1 Answer
Position problem, Make my own Button 2 Answers
OnMouseButtonDown raycast 2 Answers