I want you to create one script that will be attached to two GameObjects. The script will have two methods that will cause the GameObjects to rotate left and right. This will show you that from a single script file, two separate Component objects will be created in the memory. Each Component object is a separate instance object with no absolutely knowledge of the other.
Capsule
and Cube
.Spinner
script to the Capsule and the Cube GameObjects.LeaningScript
as shown in the following screenshot:Here's the output to the Console:
Now press the left and right arrow keys to make the Capsule spin, and the up and down arrow keys to make the Cube spin.
You created one script named Spinner
, then attached the script to two separate GameObjects. When you click on Play, two separate Spinner
Component objects are created in the computer memory. This is an example of how the Spinner
class is just a blueprint, a description, of what each Component object created will be.
To access each Spinner
Component from the LearningScript
Component, you need to know about each GameObject that the Spinner
Component is attached to.
This code is just a simple demonstration to show how Dot Syntax works. In real life, you may have each Component detect user input. On the other hand, perhaps you may want a class dedicated to processing user input. That's the neat thing about writing code, there are a zillion ways to accomplish a task.
An analysis of the code shown in the previous code screenshot is as follows:
On LearningScript
:
Line 6: GameObject capsuleGO;
Line 7: Spinner cubeComp;
Spinner
is declaredSpinner
Component object created from the Spinner
classLine 9: void Start()
Start()
method is used to allow the two variables to be initializedLine 11: capsuleGO = GameObject.Find("Capsule");
Find()
method of the GameObject
class locates a GameObject in our ScenecapsuleGO
Line 12: Debug.Log(capsuleGO);
capsuleGO
Line 13: cubeComp = GameObject.Find("Cube").GetComponent<Spinner>();
Line 14: Debug.Log(cubeComp);
Spinner
Component is part of the Cube GameObject, and is in fact referenced in the variable cubeComp
Line 19: if(Input.GetKey(KeyCode.LeftArrow)
if
statement checks to see if the user has pressed the left arrow keyLine 21: capsuleGO.GetComponent<Spinner>().SpinLeft();
CapsuleGO
variable substitutes the reference to the Capsule GameObjectSpinner
Component object is located on the Capsule GameObjectSpinLeft()
method is called in the Spinner
Component of the Capsule GameObjectSpinner
Component objectSpinner (on the Capsule):
Line 6: public void SpinLeft()
SpinLeft()
method called from the LearningScript
objectLine 8: transform.Rotate(0, 0, 60 * Time.deltaTime);
Rotate()
method on the Transform
Component object is called which causes the Capsule to spin around the z-axistransform
is used in the Dot Syntax statement instead of the GetComponent<Transform>()
methodTransform
Component classGameObject
class in the Scripting Reference and notice that one of the variables is named transform
GetComponent()
method on a GameObject, Unity has provided a convenient variable already assigned the value of the Transform
Componenttransform
variable described in the Scripting Reference:Rotate()
method shows 3 arguments being sent to the method.LearningScript
object.Line 24: if(Input.GetKey(KeyCode.RightArrow)
if
statement checks if the user has pressed the right arrow keyLine 26: capsuleGO.GetComponent<Spinner>().SpinRight();
Line 29: if(Input.GetKey(KeyCode.UpArrow)
if
statement checks if the user has pressed the up arrow keyLine 31: cubeComp.SpinLeft();
cubeComp
variable already stores the reference to the Cube GameObject and the Spinner
Component object, thereforeJust the variable cubeComp
is needed in the Dot Syntax to call the SpinLeft()
method on the Cube GameObjectLine 34: if(Input.GetKey(KeyCode.DownArrow)
if
statement checks to see if the user has pressed the down arrow keyIn LearningScript
, lines 21 and 31 perform the same functionality of calling the SpinLeft()
method on their Spinner
Components. Yet the code on each line is very different. The difference is that cubeComp
already stores a reference to the Cube's Spinner
Component. There is no capsuleComp
variable to store a reference to the Capsule's Spinner
Component.
Try creating a capsuleComp
variable and store a reference to the Capsule's Spinner
Component. Then change lines 21 and 26 to use capsuleComp
.
18.224.54.255