Game Development Reference
In-Depth Information
The messages show that the new value assignment was successful (Figure 5-33 ).
Figure 5-33. The message reflecting the new value for favoriteFood
Identifying a component of known type, such as the Camera, is a matter of adding the component
with dot notation. Let's assign the Main Camera to the three test objects.
Add the following code beneath the print line:
5.
theView = GameObject.Find("Main Camera").camera;
Assigning a component that is not listed as a variable for the GameObject class (see Figure 5-17 ) is
handled using GetComponent() , a generic function. But because everything must be “typed” in C#,
you will have to specify the type rather than just provide the class name. As before, you must also
specify which gameObject's component you want. Let's use the Walkway object for this example.
Add the following code beneath the theView line:
6.
myCustomScript = GameObject.Find("Walkway").GetComponent<VariablesTest>();
7.
And now, add a print statement to check the results:
print ("The view uses " + theView.gameObject.name);
print ("The script is from the " + myCustomScript.gameObject.name + " object");
Note that this time you start with the script, but it's the gameObject's name you want, not the script's
name, so you traverse the object hierarchy up to the parent using dot notation before asking for its
name, myCustomScript.gameObject.name .
8.
Save the script, (Figure 5-34 ).
 
Search WWH ::




Custom Search