Game Development Reference
In-Depth Information
6. In
Level3Init
, we first try to find a reference to a GameObject named
MainCamera
:
GameObject go = GameObject.Find
("MainCamera");
7. If the
MainCamera
GameObject can be found, we try to cache a reference
to the
PopupMgr
script component attached to it:
if (go)
{
PopupMgr ppm =
go.GetComponent<PopupMgr>();
8. If the
PopupMgr
script can be found, we set the initial state of the UI
pop ups for level 3. As before, we use
SetActive(false)
to suspend
the
Update()
loop of some scripts and
SetActive(true)
to enable
others. Namely, we deactivate
popup_Level3Finish
and
popup_Level3Restart
. We then activate
popup_Level3Start
so that
the level starts with the relevant UI displayed. Note that at this point, the cam-
era is looking up from
popup_Level2Finish
:
if (ppm)
{
ppm.Level3Finish.SetActive (false);
ppm.Level3Repeat.SetActive(false);
ppm.Level3Start.SetActive(true);
9. Next, we try to cache a reference to
PopupButtonScript
, which is at-
tached to
button1
child of
popup_Level3Start
. We use
trans-
form.FindChild()
to search for an object in a hierarchy by name. Once
we find it, we can get the component for
PopupButtonScript
itself using
GetComponent()
:
PopupButtonScript pbs =
ppm.Level3Start.transform.FindChild
("Button1").gameObject.GetComponent<PopupButtonScript>();