Time for action – creating two GameObjects and a new script

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.

  1. In your Scene, create two GameObjects, Capsule and Cube.
  2. Add a Directional Light to the Scene so you can easily see the GameObjects.
  3. Here's my Scene as an example:
    Time for action – creating two GameObjects and a new script
  4. Create a new C# Script and name it Spinner.
  5. Code the script as shown in the following screenshot:
    Time for action – creating two GameObjects and a new script
  6. Attach the Spinner script to the Capsule and the Cube GameObjects.
  7. Modify LeaningScript as shown in the following screenshot:
    Time for action – creating two GameObjects and a new script
  8. Save the file.
  9. In Unity, click on Play.

What just happened?

Here's the output to the Console:

What just happened?

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.

Note

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;

  • A variable of type GameObject is declared
  • The value this will store is a reference to the Capsule in the Scene

Line 7: Spinner cubeComp;

  • A variable of type Spinner is declared
  • The value this will store is a reference to a Spinner Component object created from the Spinner class

Line 9: void Start()

  • The Start() method is used to allow the two variables to be initialized
  • Remember, this method is called only once

Line 11: capsuleGO = GameObject.Find("Capsule");

  • The Find() method of the GameObject class locates a GameObject in our Scene
  • The reference to the Capsule GameObject is assigned to the variable capsuleGO
  • This is item 5 in the previous graphic and also on the previous code screenshot

Line 12: Debug.Log(capsuleGO);

  • This line was added just to show that the Capsule GameObject is in fact referenced in the variable capsuleGO

Line 13: cubeComp = GameObject.Find("Cube").GetComponent<Spinner>();

  • This line shows how to retrieve a Component on a GameObject
  • This retrieved reference to the Spinner Component object is on the Cube GameObject
  • This is item 6 on the previous graphic and also on the previous code screenshot

Line 14: Debug.Log(cubeComp);

  • This line was added just to show that the Spinner Component is part of the Cube GameObject, and is in fact referenced in the variable cubeComp

Line 19: if(Input.GetKey(KeyCode.LeftArrow)

  • This if statement checks to see if the user has pressed the left arrow key
  • If pressed, Line 21 of the code block is executed

Line 21: capsuleGO.GetComponent<Spinner>().SpinLeft();

  • This line shows using Dot Syntax to locate a method in a Component of another GameObject.
  • The CapsuleGO variable substitutes the reference to the Capsule GameObject
  • The Spinner Component object is located on the Capsule GameObject
  • The SpinLeft() method is called in the Spinner Component of the Capsule GameObject
  • Code flow now jumps to the Spinner Component object

Spinner (on the Capsule):

Line 6: public void SpinLeft()

  • This is the SpinLeft() method called from the LearningScript object
  • Line 8 in the code block is executed

Line 8: transform.Rotate(0, 0, 60 * Time.deltaTime);

  • The Rotate() method on the Transform Component object is called which causes the Capsule to spin around the z-axis
  • Notice though, that the variable named transform is used in the Dot Syntax statement instead of the GetComponent<Transform>()method
  • Unity has several built-in Components, such as the Transform Component class
  • Find the GameObject class in the Scripting Reference and notice that one of the variables is named transform
  • Instead of having to use the GetComponent() method on a GameObject, Unity has provided a convenient variable already assigned the value of the Transform Component
  • The following screenshot shows the transform variable described in the Scripting Reference:
    What just happened?
  • The Rotate() method shows 3 arguments being sent to the method.
  • In this example, the Capsule is rotating 60 degrees per second on the z-axis.
  • Code flow now returns to the LearningScript object.

On LearningScript:

Line 24: if(Input.GetKey(KeyCode.RightArrow)

  • This if statement checks if the user has pressed the right arrow key
  • If pressed, line 26 of the code block is executed

Line 26: capsuleGO.GetComponent<Spinner>().SpinRight();

  • This is almost an exact repeat of line 21, except the SpinRight() method is being called

Line 29: if(Input.GetKey(KeyCode.UpArrow)

  • This if statement checks if the user has pressed the up arrow key
  • If pressed, line 31 of the code block is executed

Line 31: cubeComp.SpinLeft();

  • This is different than lines 21 and 26
  • Refer back to line 13. The 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 GameObject
  • Code flow is similar to line 8, except that the Cube rotates now

Line 34: if(Input.GetKey(KeyCode.DownArrow)

  • This if statement checks to see if the user has pressed the down arrow key
  • If pressed, line 36 of the code block is executed, spinning the Cube right

Have a go hero – creating and using a new variable named capsuleComp

In 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.255