- Home /
yield new wait for seconds: what does "new" do?
I was experimenting with the Batching iPhone Example in the Example Projects section of the Unity webpage. There is a script called bullsEyeAnimationCycle.js:
function Awake ()
{
playBullsEyeAnimation();
}
function playBullsEyeAnimation ()
{
animation.Play("reveal");
yield new WaitForSeconds (Random.Range (0.5, 2.0));
animation.Play("hide");
yield new WaitForSeconds (animation["hide"].clip.length);
Destroy (transform.root.gameObject);
}
What is the role of "new" after yield? I couldn't find anything in the Scripting Overview. Thanks :)
Answer by Mike 3 · Jan 02, 2011 at 10:17 AM
It creates a new instance of the type you put after it.
e.g.
var x = new Vector3(1,2,3);
creates a new Vector3 instance.
In javascript it's generally optional, with the exception of arrays:
var x = new Vector3[8];
requires the new.
WaitForSeconds is actually a class which unity uses in coroutines for checking how long it should wait
so if the "new" wasn't there, wouldn't the engine still WaitForSeconds and play the animation / destroy the gameObject?
Your answer
Follow this Question
Related Questions
Why is my delay not working? 2 Answers
How can I use a co-routine to yield player damage? 1 Answer
Take damage after seconds 3 Answers
Have a function to Wait until true? 4 Answers
Using yield to pause a function and start another one. 1 Answer