Time for action – communicating with another Component on the Main Camera
Let's create another script with a variable and a method, and attach it to the Main Camera, then have LearningScript
communicate with it:
In Unity, create another C# Script and name it
TalkToMe
.Make a
public
string
variable namedhereItIs
.Assign some text to
hereItIs
.Make a
public
method namedMakeMeTalk()
.Have
MakeMeTalk()
output some text to the Console.Attach
MakeMeTalk()
to the Main Camera. Now the code should look something like this:Modify
LearningScript
to retrieve theTalkToMe
Component.Modify
LearningScript
to retrieve the data inhereItIs.
Modify
LearningScript
to call theMakeMeTalk()
method. Now the code snippet should look as follows:Save your scripts.
Click on Play in Unity.
What just happened?
Here's the output:
The LearningScript
Component code retrieved a variable and called a method on the TalkToMe
Component. Let's follow the code flow with these two Components.
An analysis of the code shown in the previous...