Let's create a simple script to update the text field of this score as shown in the following screenshot:
The following script will update the text field in the upper-right corner of the screen so that it reflects the score stored in PlayerData:
Switch to the MAIN scene. Create a new script, and name it
scoreScript.cs
.Attach an instance of this script to the
score
GameObject.In the start method of this script, find the GameObject named
Player
, and then store a reference to thePlayerData
component on this script.The
score
script needs to update a member of theGUIText
GameObjectscore
, so the update loop that this script should be called from isOnGUI()
. Inside this loop, we check for aPlayerData
component as shown in the following code:void OnGUI() { } GameObject go = GameObject.Find("Player")
If there is one, we take the score value from
PlayerData
and assign it to the text field of the textbox. Note that we have to use the C# helper function.ToString()
to convert...