Time for action – accessing a variable in the current Component
Let's look at accessing a variable in LearningScript
from inside LearningScript
.
- Modify
LearningScript
as shown in the following figure: - Save the file.
- In Unity, click on Play.
What just happened?
Here are the outputs in the Console:
An analysis of the code shown in the previous code screenshot is as follows:
Line 6: string myString = "Access the variable ";
myString
is the variable that will be accessed- Notice that it's
private
by default, yet it can still be accessed
Line 17: Debug.Log(myString ...
Get Learning C# by Developing Games with Unity 3D Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.