We'll have three cameras, but only one will be active at a time. Their names are:
Do the following steps with Scene0 loaded to add two cameras:
Look in the Scripting Reference for GameObject.SetActive. It's a method to set whether a GameObject is active or not. However, Unity can't find an inactive GameObject using the
GameObject.Find()
method. So we'll store a reference to each camera in a List.
Line 10 in the following screenshot of the GameData
script creates the List to store the three cameras:
Now we'll add all the cameras in Scene0 to the List:
3.
Now that we have references to all cameras, whether they are active or not, we have the ability to make a camera active in code.
PlayStateScene1_1
in MonoDevelop.Let us analyze the code that we just saw:
Line 21: foreach(GameObject camera in manager.gameDataRef.cameras)
foreach
loop retrieves each camera in the List named cameras
camera
Line 23: if(camera.name != "LookAt Camera")
foreach
loop iterates through the List, if the camera retrieved is not named LookAt Camera, then line 24 is executedLine 24: camera.SetActive(false);
Line 26: camera.SetActive(true);
Switching to PlayStateScene1_2
makes the Following Camera active:
As this foreach
loop iterates though the List, LookAt Camera will be set as inactive, and Following Camera will be set to active.
18.118.2.225