Book Image

Creating E-Learning Games with Unity

By : David Horachek
Book Image

Creating E-Learning Games with Unity

By: David Horachek

Overview of this book

Table of Contents (17 chapters)
Creating E-Learning Games with Unity
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Making the ScorePlate active


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:

  1. Switch to the MAIN scene. Create a new script, and name it scoreScript.cs.

  2. Attach an instance of this script to the score GameObject.

  3. In the start method of this script, find the GameObject named Player, and then store a reference to the PlayerData component on this script.

  4. The score script needs to update a member of the GUIText GameObject score, so the update loop that this script should be called from is OnGUI(). Inside this loop, we check for a PlayerData component as shown in the following code:

    void OnGUI() { }
    GameObject go = GameObject.Find("Player")
  5. 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...