Time for action – accessing the scripts on other objects
The only existing script on our marble right now is the input script, so create another one that will be responsible for handling the collection data. Perform the following steps to access the scripts:
Create a new file named
PlayerCollection.cs
in yourScripts
folder and open it in your code editor.Add the following variable and function to your code, above and below the
Start
andUpdate
functions, respectively:private int totalCoins = 0; // Use this for initialization void Start() { } // Update is called once per frame void Update() { } public void CollectCoin() { totalCoins++; print(totalCoins); }
Let's look at the two things you just added to your script. We set the
int
variable toprivate
because this script is the only one that needs to edit that value, but we added apublic
function that increments the value by one.This method is better than directly accessing a public variable on another script because making variables...