I’m quite new to Unity3D but I’m a professional programmer so most things don’t look alien to me
I know the yield statement from C# and in .NET dev it is highly used as a way into co-routines, so I was glad that it is available in UnityScript too, but I don’t seem to understand it…
It is acting completly different from what I rht was thinking and from what I can read in the ref guide: http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html
Say that I have a function OnTriggerStay which needs to do some things.
From what I know about co-routines (and yield) this should give me this output:
first part
second part
third part
fourt part
What it is giving me is completly unpredicatable, it’s giving me like first part a few times and then it gives me second part a few times only to print first part a few times again. Maybe it is the combination between the yield statement and the OnTriggerStay, since running this from the Start gives me correct behaviour…
Maybe somebody can help me wrap my head around this?
works fine for me…? it think your problem is the collision, you probably collide multiple times with your object which keep resetting the function… In my case i made an empty trigger and walk trough it… also you say in OnTriggerStay but used OntriggerEnter…?
hope this helps
Thnx 4 the answers, I was just wondering why it was acting like this.
I already provided a latch in my tests to prevent it from running multiple times, but I was looking for a reason why it was running multiple times
Seems like the co-routine is reset every time even when the collision is between the same objects.
I was following along with the Walker Brothers their tutorial and they’re doing some strange things, which led me to this test and left me wondering why the co-routine was “reset” every time.
So if I understand it correctly, every (virtual) (hook) method will reset the co-routine every time it’s entered?
Hi guys, I know that this post is a bit old but want to make a comment. I’ve been 2 days driving to insanity with coroutines, got me mad. The same thing happening to DwightM is happening to me. I want to achieve a simple funcionality, the font size of a text must grow from 1 to 65 and then back to 1, that must happen in 4 seconds, so I have 2 seconds to go to 65 and 2 to back to 1. To put the thing simple, let’s say I want to make that in 4 steps, so in 1 second I’ll have the font size 32, in 2 seconds 65 and so on. I’ve used coroutines to achieve this, using UnityScript “yield WaitForSeconds(1.0)” but results are terrible unpredictable, font grows 'till around 1500, then back to 600 and get killed by my code “Destroy(gameObject)”, seems that “yield WaitForSeconds(1.0)” is not working fine, here’s my code(note that I’m not showing you the declaration of some variables, just the important ones):
var steps : int = 2;
var tiempoMuerto : float = 1.0;
function OnGUI () {
dibujarFlor();
}
function dibujarFlor () : IEnumerator {
// aca sube la fuente
for(var counter : int = 1; counter <= steps; counter ++) {
ownStyle.fontSize += incrementoPorStep;
GUI.Label ((Rect(x, y, 100, 100)), caracter, ownStyle);
yield new WaitForSeconds(tiempoMuerto);
}
// aca baja la fuente
for(counter = 1; counter <= steps; counter ++) {
ownStyle.fontSize -= incrementoPorStep;
GUI.Label ((Rect(x, y, 100, 100)), caracter, ownStyle);
yield new WaitForSeconds(tiempoMuerto);
}
// desaparece
RandomFlowers.floresMostradas --;
Destroy(gameObject);
}