Book Image

Unity 2D Game Development Cookbook

By : Claudio Scolastici
Book Image

Unity 2D Game Development Cookbook

By: Claudio Scolastici

Overview of this book

<p>Unity is a powerful game development engine that provides rich functionalities to create 2D and 3D games.</p> <p>Unity 2D Game Development Cookbook is a practical guide to creating games with Unity. The book aims to serve the purpose of exploring problematic concepts in Unity for 2D game development, offering over 50 recipes that are easy to understand and to implement, thanks to the step-by-step explanations and the custom assets provided. The practical recipes provided in the book show clearly and concisely how to do things right in Unity. By the end of this book, you'll be near "experts" when dealing with Unity. You will also understand how to resolve issues and be able to comfortably offer solutions for 2D game development.</p>
Table of Contents (15 chapters)
Unity 2D Game Development Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Instantiating a particle system at runtime


In this recipe, we take care of instantiating a particle system prefab in the place of a collected item that disappears, and also use the particle system to play the sound effect we set for this specific game event.

To achieve this, we need a new script and need to make some modifications to the Runner script, so let's begin.

Getting ready

Keep your project ready to add the new assets we need.

How to do it

  1. Create a new C# script in the Scripts folder of your project and name it PS_Manager.

  2. Edit the Update() function so that it contains the following lines. They tell the PS to destroy itself once the audio attached to it has finished playing:

    void Update () {
      if(!audio.isPlaying){
        Destroy(this.gameObject);
      }
    }
  3. Now open the Runner script in Monodevelop, if you did not open before.

  4. To begin with, we need a public GameObject variable to store the reference to the particle system prefab we are going to create in the scene. Add the following line to the...