- Home /
what's wrong with unity
My unity version is 3.1.0f4, and my CPU is Intel Core i5. I write these code in one java script's update():
print("in State_BeingShoot()"); transform.animation.Play("hit"); yield WaitForSeconds(1); mLife--; print("being shot mLife" + mLife);
There are two print statements, but in my log, I see that just one of them can be printed in one circle. By the way, there are someting wrong with the update() before: I write one print statement in update, it just print once. But when I write a variable and add 1 to it per cycle, it is printed normally. So what's the matter. Thank you for taking time reading this!
Answer by testure · Jun 23, 2011 at 04:11 AM
Update can't be used as a coroutine:
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
specifically:
Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can.
Thank you! So, If I want the script to wait for a while, which function shoud I use? I want to use the function to wait the animation finishing playing.
start the animation in a coroutine, and then start the coroutine from Update.
I don't use Javascript, so I can't give you code that you can use via copy/paste, but if you take a look at it, you should be able to interpret what i'm doing:
IEnumerator DoAnimationThing()
{
animation.CrossFade("my animation");
yield return new WaitForSeconds(2f);
Debug.Log("this happens 2 seconds later.");
}
void Update()
{
if(startAnimation)
{
StartCoroutine(DoAnimationThing());
}
}
Javascript uses dynamic typing, so you obviously won't need the IEnumerator or void types.. in fact I don't think you have to use 'new' when returning WaitForSeconds either.. but yeah, look some of that stuff up on the unity docs and you'll get clear examples of how to do it in javascript.
Your answer
![](https://koobas.hobune.stream/wayback/20220612115040im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Spawing a SpawnPoint with Raycast 0 Answers
How to implement Aggro into a AI script (Js) 0 Answers
NGUI panel wont hide 2 Answers
Main menu camera change script. 1 Answer
How draw a path using touch in c# 1 Answer