Game Development Reference
In-Depth Information
Classified intel
In our script, we need to wait for a second between opening the door and ending the game.
We could do this by looping or performing some other task for a second, but that would
stop the animations, the sound, and everything else. We get around this by using the
yield command and the Coroutines() function.
Coroutines
The yield command tells Unity to stop running our function and come back later (in our
game, one second later as we call yield WaitForSecond(1) (Unity JavaScript) or
yield return new WaitForSecond(1) (C#). By using the yield command, our
function becomes Coroutines and now it must return IEnumerator (Unity needs this
so that it can tell when to start our function again). This means Coroutines can't return a
value like a normal function. We can change most functions in our MonoBehaviours
script into Coroutines , apart from the ones that already run in every frame, such as Up-
date() , FixedUpdate() , and OnGUI() . We can get more information about
Coroutines from the following Unity script reference: http://unity3d.com/support/docu-
mentation/ScriptReference/Coroutine.html
The Restart button
If we take a look at the TextureButton script, we will see that it uses the mouse event
function, which will check for the mouse roll over, mouse roll out, and mouse up events. In
the OnMouseUp() function, we will see that we create the new player and key by using
Instantiate to clone both prefab objects and set it back in the scene.
We can also add Application.LoadLevel(LevelName) to reset our game, which
is much easier than using Instantiate , but Application.LoadLevel will destroy
all the game objects in the scene and reload again.
In this case, we use Instantiate in our game because we only have one scene and don't
want to load the whole game level again. However, we can also put DontDes-
troyOnLoad() in the Awake() function of the object that we don't want to destroy, but
it needs a bit of setting up. So, there is no right or wrong. It depends on what we want to
use or where we want the project to go.
Search WWH ::




Custom Search