Do “yield return time();”, except you might as well just use WaitForSeconds directly instead of putting it in another function, since that doesn’t really serve any purpose.
I did try to do it directly but it couldn’t compile. An error came out e.g. “cannot be an iterator block because `void’ is not an iterator interface type”
So after reading some stuff online, it says I have to make the type as IEnumerator. The problem was that my main update function is void. So I then made:
void Update(){
gameStart();
}
IEnumerator gameStart()
{
//my codes here
yield return new WaitForSeconds(5f);
}
I think the second one had no errors but it couldn’t get any input. Mine uses a swipe function. What I aim to do is something like:
//just a pseudocode
while(idle == true)
{
play.Anim("idle01");
yield WaitForSeconds(5);
play.Anim("idle02");
loop process again
}
if(swipe)
{
play.Anim("laugh01");
yield WaitForSeconds(5);
play.Anim("laugh02");
loop process again
}
Not sure why it’s not working. Any help would be appreciated.
You can’t yield in Update, because it runs every frame without exception. If you start a coroutine in Update, you’ll be starting a new instance of the coroutine every frame. So, basically don’t use Update if you need to yield stuff.
Yep I noticed that one. But for some reason(in JavaScript) it’s achievable but only if I create a separate function e.g. void gameStart(); and call it inside update.
It works fine in Javascript but I just encountered a bit of a bug.I’ll see what I can do
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.tag == "planeRight")
{
print("Plane Right");
yield WaitForSeconds(5);
print("After 5 seconds");
}
if(hit.collider.tag == "planeLeft")
{
print("Plane Left");
}
}
}
if (Input.GetMouseButtonUp (0)) //Returns true during the frame the user deselects the object
{
if (Physics.Raycast (ray, hit, 100))
{
print("Idle");
}
}
else {
}
I might be wrong at coding this but what I wanted to do is to show print “after 5 seconds” only when I press and hold the mouse button down. The way this one works now is that by just clicking on it once it will go to “idle” > “after 5 seconds” Should I use a while loop?
Hmm, for that case I usually use a float variable and assigning Time.time to it. Example:
When the button down event is fired for exactly one frame, you set a variable buttonDown to Time.time:
var buttonDown : float = 0.0;
if (Input.GetMouseButtonDown(0)) {
buttonDown = Time.time;
}
Inside the GetMouseButton function which is fired all the time the button is pressed down, I would check if the buttonDown Time + 5.0 seconds has passed.
if (Input.GetMouseButton(0)) {
if ((buttonDown + 5.0) > Time.time) {
buttonDown = 0; // to not run into this if case again
// Display something
}
}
The danger with using yield WaitFor… what you wrote above is, that the coroutine that you fired during the buttonDown event, but when the user releases the button, the coroutine is still there and running. So that is at least how I would do it with a timer variable. Hope it helps.
Smag: I’ve tried using caching the Timed Function but it was still giving me the same effect. Instead of waiting 5 seconds to print something, it printed something straight away. When you said cache, it means I have to put the StartCoroutine(“Timed”) in void start() right?
Martin Schultz:
Not sure how the time.time works. When I click on it, it should have printed out “plane right” > after 5s > print “after 5 seconds”. Except with this one, it printed out “after 5 seconds” on the first click and print out “plane right” after. Also, is the value in seconds for (buttonDown + 5.0) > Time.time) ?
Here’s what I did for the JavaScript part:
if (Input.GetMouseButtonDown(0)) {
buttonDown = Time.time;
}
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.tag == "planeRight")
{
print("Plane Right");
if (Input.GetMouseButton(0))
{
if ((buttonDown + 5.0) > Time.time) {
buttonDown = 0; // to not run into this if case again
print("After 5 seconds");
}
}
}
if(hit.collider.tag == "planeLeft")
{
print("Plane Left");
}
}
}
Just in case I’ve written in a confusing way, I’m basically trying to get a game character to play some idle animations e.g. In the old Sonic games, when you leave him for a few seconds, he’ll start tapping his shoes and when you move him it basically resets the timer again to 0.
Once that is done, I think I’d like to extend it further e.g. When I click and hold the mouse button, the character will fire(animation1) and after 5 secs while still holding, will play another set of animation2.
Hope that makes sense. I know I might be able to code this but I think I’m getting the game logic wrong
I use something like that all the time and it should work. There’s no way it should print something right away when you do it like that. It’s either very weird, or you did something wrong
And no, with caching I mean to create a class variable (possibly static) of the string “Timed” so you can reuse it, so memory is not increased each time you need that string (which will cause the GC to kick in). That’s not a necessary step, just an optimization. A suggestion I added.
Hmm I might have done something wrong then. I’ll post the code if I still can’t get it to work. Even without caching, would the yield time still work?
As for the initial code, I placed theStartCoroutine(“Timed”); inside void Start() and placed the Timed function inside the void Update(). Not sure if I should place it someplace else but when I did, it kept coming out with an error saying missing “;”
Thanks again.
-Hakimo
p.s. A friend showed me Captain Ludwig the other day. Just realised in your signature that you made it. Awesome game mate
If you call Timed() in the Update function it will not be started as a coroutine and just as a function, and hence it will execute immediately.
If you want to use yield, you need to call it with StartCoroutine. So put the StartCoroutine where you want to call it (in your case in the update function), and not in the Start method.