Game Development Reference
In-Depth Information
13. Before we are done, we need to add
doorOpen.png
and
doorClose.png
to the
Player
. Go back to Unity, and click on the
Player
; in the
Inspector
view now, we
will see two new parameters,
Door Open Texture
and
Door Close Texture
; drag-and-
drop
doorOpen.png
to
Door Open Texture
and
doorClose.png
to
Door Close
Texture
. Now we are done.
Click
play
and try out your game, collecing the key and going to the door. Behold the door
opening and closing!
Objective Complete - Mini Debriefing
We just created a
key
and
door
object, and placed them at our level. We also created
the funcion that will trigger when the character hits the
key
and
door
objects. Then, we
changed the texture of our
door
object when our character had a
ke
y object and hit the
door. Lastly, we waited for one second to remove our character from the scene and changed
the
door
texture back to closed state by using
yield
and
Destroy
.
Classified Intel
We can pause or wait for the next acion by using
coroutines
.
Coroutines
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 animaions, the sound, and everything else. We get around this by using the
yield
command; this tells Unity to stop running our funcion and come back later (in our game,
1 second later as we call
yield WaitForSecond(1)
). By using the
yield
command our
funcion becomes
Coroutines
and now it must return
IEnumerator
(Unity needs this
so that it can tell when to start our funcion again). This means
Coroutines
can't return a
value like a normal funcion. We can change most funcions in our
MonoBehaviours
script
into
Coroutines
, apart from the ones which already run in every frame, such as
Update()
,
FixedUpdate()
,
OnGUI()
, and so on. We can get more informaion about
coroutines
from the following Unity script reference:
http://unity3d.com/support/documentation/ScriptReference/Coroutine.
html
.