- Home /
Yield refuses to work. Js
Can someone help me, yield won’t work for me, this is the second project it won’t work in, and I’ve tried reinstalling too as well as installing previous versions of Unity cause I do not see what I'm doing wrong.
function Lose()
{
print(“before yield”);
yield (WaitForSeconds(2));
print(“after yield”);
Time.timeScale = 0;
guiMode = “Lose”;
}
“before yield” prints out, but “after yield” never shows up and neither does anything after it. However if I remove the yield line all together everything works except the explosion doesn’t get to show that way…
I tried all variations of yield I can think off and none work.
I tried like the one in the tutorial video (Mine Lander UnityCookie) and:
yield WaitForSeconds(2);
and
yield (WaitForSeconds(2.0));
and
yield WaitForSeconds(2.0);
but none work, only “before yield" prints out and no menu pops up and time doesnt freeze. And like I mentioned, it didn't work in another project either.
Any ideas, or alternative ways of waiting a couple sec?
Answer by ChrisSch · Jul 15, 2013 at 04:59 AM
Ok solved it.
Since I had another object calling the function Lose, which I didn't mention cause I didn't think it was important, and the object calling it got destroyed after calling it, the Lose function stopped executing all together. I thought I just have to call it before destroying the object and it would work fine, like it does in the tut, but it doesn't. xD
Anyway in case someone needs it, I solved it by adding another function, LoseCaller onto the GUI Object that doesn't get destroyed, as a relay even tho that one never gets destroyed in any case. Guess it kind of makes sense now why it wouldn't work... Oh well. xD
Ship Object:
var GUI : InGameGUI;
function Start ()
{
GUI = GameObject.FindWithTag("GUI").GetComponent(InGameGUI);
}
function Explode()
{
var randomNumber : int;
randomNumber = Random.Range(0,shipExplossions.length);
Instantiate(shipExplossions[randomNumber], transform.position,transform.rotation);
GUI.LoseCaller();
Destroy(gameObject);
}
GUI Object:
function Lose()
{
print("before yield");
yield (WaitForSeconds(2));
print("after yield");
Time.timeScale = 0;
guiMode = "Lose";
}
function LoseCaller()
{
Lose();
}
just want to confirm this worked for me! Thanks for finding out this! Although this should not be normal.......(or hasnt been before..) Is this the new standard..... or a bug?!?!
Its a new "we're still noobs and don't know exactly what we're doing so somewhere else in our script might be the problem" xD
Or its a bug, I really don't know cause I followed the tutorial to the letter and it didn't work but when I tried a simpler example it did, so no idea if bug or new standard or both. xD
my code has always worked for me... until i recently upgraded to Unity 4.2.... Now i gotta relay....>.<
I guess its a bug then. xD $$anonymous$$y Unity works better as of 4.2, and I'm loving the one directional light with shadows. xD Are you doing this tutorial too or something else?
Answer by Eric5h5 · Jul 14, 2013 at 10:45 PM
Change timeScale to 1.0 before doing the yield.
It was 1 since the explosion happens, but even so I changed it before yield again, and nothing new happens. It never passes the yield line.
Your answer
