Book Image

OUYA Game Development by Example

By : John Donovan
Book Image

OUYA Game Development by Example

By: John Donovan

Overview of this book

The OUYA console and development kit gives you the power to publish video games for the players, creating a console marketplace of the gamers, for the gamers, and by the gamers. Using the OUYA developer kit and the Unity3D game engine, even beginners with a captivating game idea can bring it to life with a hint of imagination. OUYA Game Development by Example uses a series of feature-based, step-by-step tutorials that teach beginners how to integrate essential elements into a game engine and then combine them to form a polished gaming experience.
Table of Contents (18 chapters)
OUYA Game Development by Example Beginner's Guide
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Create a new file named PlayerCollection.cs in your Scripts folder and open it in your code editor.

  2. Add the following variable and function to your code, above and below the Start and Update 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 to private because this script is the only one that needs to edit that value, but we added a public function that increments the value by one.

    This method is better than directly accessing a public variable on another script because making variables...