Game Development Reference
In-Depth Information
11. If a suitable player object is found, the code sets the initial position and the
orientation for the player. This is so that when level 2 starts, the player begins
at the starting line of the race:
if (playerObj != null)
{
playerObj.transform.position =
new Vector3(-110.0f, 3.0f, 166.0f);
p.GetComponent<playerControls>().moveDirection
=
new Vector3(1.0f, 0.0f, 0.0f);
}
12. In the
Update()
method of
Level2Init
, the class attempts to find the
MainCamera
GameObject. If it can be found, the
PopupMgr
script compon-
ent is stored:
GameObject camObj = GameObject.Find
("MainCamera");
if (camObj)
{
PopupMgr ppm =
camObj.GetComponent<PopupMgr>();
13. If an instance of the
PopupMgr
script can be found on this GameObject,
we activate
popup_Level2Start
and deactivate
popup_Level2Finish
and
popup_Level2Repeat
. By setting these objects to active, we tell Unity
that these scripts should be allowed to call their internal
Update()
methods.
Conversely, when setting
SetActive(false)
, we can tell Unity to suspend
scripts as needed:
// set up the level2 popups initial state
ppm.Level2Finish.SetActive(false);
ppm.Level2Repeat.SetActive (false);
ppm.Level2Start.SetActive(true);
14. Then we store a reference to the
PopupMgrScript
on the
popup_Level2Start
GameObject for later use: