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.

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.    
}