- Home /
Why is this check executing twice?
I have two floats, currentMinute and lastMinute, which I check every time my timer class advances time, and whenever the minute value of the timer is incremented I want to fire an event OnUpdateMinute:
public void CheckMinutes(float currentMinute){
if (currentMinute != lastMinute) {
lastMinute = currentMinute;
if (OnUpdateMinute != null)
OnUpdateMinute();
}
}
The only thing subscribed to this event is a test function that sends a line to Debug.log, but every time the minute changes that function gets run twice, not once- is there an obvious flaw I'm missing here?
So I have absolutely no idea how that happened (or I missed it), but you're absolutely right about there being two instances of the script for some reason. 0_0
It works perfectly now, thank you!! :)
glad to help. thing could get hectic and you'd lose track of stuff after awhile =D
Answer by MiniFish · Jan 05, 2015 at 06:30 AM
multiple instances of the script, maybe? try printing out the name of the game object that's calling this function and maybe you'll be able to catch the other script. otherwise your timer class could be calling this function multiple times instead.
Answer by Skeledurr · Jan 05, 2015 at 07:29 AM
You're possibly listening to the event multiple times.
Try unregistering the handler before you register it.
OnEnable()
{
yourClass.OnUpdateMinute -= OnUpdateMinute;
yourClass.OnUpdateMinute += OnUpdateMinute;
}