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.
In your Scene, create two GameObjects,
Capsule
andCube
.Add a Directional Light to the Scene so you can easily see the GameObjects.
Here's my Scene as an example:
Create a new C# Script and name it Spinner.
Code the script as shown in the following screenshot:
Attach the
Spinner
script to the Capsule and the Cube GameObjects.Modify
LeaningScript
as shown in the following screenshot:Save the file.
In Unity, click on Play.
What just happened?
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...