- Home /
Use yield WaitForSeconds to delay mouseover on a gameObject? (JS)
I have a main menu which has a 3 second transition in, which looks cool and all, but I ran into a problem - I can mouseover the objects during their transition in, which I don't want to be able to do. So I used a yield WaitForSeconds(3) before the function OnMouseOver, thinking that would solve it:
iTween.MoveFrom(gameObject, {"y":5, "time":3, "EaseType":"EaseOutSine"});
iTween.RotateFrom(gameObject, {"y":-5, "time":5, "EaseType":"easeInOutQuad", "loopType":"pingPong"});
//wait for transition to finish to avoid glitches with mouseover
yield WaitForSeconds (3);
function OnMouseOver ()
{
iTween.MoveTo(gameObject, {"z":-.25,"time":1});
if(Input.GetMouseButtonDown(0))
{
Application.LoadLevel("Credits");
}
}
function OnMouseExit ()
{
iTween.MoveTo(gameObject, {"z":0, "time":1});
}
But it didn't appear to create any sort of delay, and the problem still existed. Is there another way to force this delay before a function without putting a delay within the function itself?
Answer by Joshua · Jul 10, 2011 at 05:05 PM
Forget the yield. Check how long the game has been running instead.
var delayTime : float = 3.0;
function Start()
{
delayTime += Time.time;
}
function OnMouseOver()
{
if( Time.time < delayTime )
return; //exit the function if it's 3 seconds since this script started
}
It also appears you yield outside of any function? That does.. nothing. The reason the iTeen functions work is because you're starting coroutines.
Thanks - that solved it. And yes, I have no idea what I'm doing sometimes. I kind of just assumed if one thing worked outside of a function, anything would.
We've all been there at some point. ;)
Functions are executed when they are called. The update function is called once every frame. The On$$anonymous$$ouseOver function is called every frame when the mouse is over the collider. Variables declared outside of functions are global variables, and can then be used inside those functions. If you call a function outside of any function you're calling a coroutine at start. This is actually not good practice, it's better to put them in the Start function, which will then call them at start.
Your answer
Follow this Question
Related Questions
Yield WaitForSeconds doesn't work 1 Answer
Can I not use yield waitForSeconds in a class function? 1 Answer
yield WaitForSeconds don't work 1 Answer
Waypoint / Yield help 1 Answer
Coroutine a function within a loop? 1 Answer