Unity3D – OnMouseDown – SetActive – Shuriken Particle System – Javascript

Unity’s Shuriken Particle System is easy to drive via code!

We will create a simple Asteroid object taht will explode OnMouseDown

Create a scene with:

– Main Camera

– (parent) EmptyObject -> name it Asteroid -> ADD a Box Collider (or OnMouseDown will not work!)
-> Transform Reset

– (child) Mesh -> your mesh -> Transform Reset

– (child) Particle System (MAIN TOP MENU> GameObject> Create Other> Particle System)
-> Inspector> Play On Awake active
-> Transform Reset

Attach to Asteroid (EmptyObject) the script:


#pragma strict

var myParticles : GameObject; // Assign in Inspector

function Start () {
   // Prefab Esplosione, disattivo altrimenti esplode subito alla creazione 
    myParticles.SetActive(false); 
} // END Start

function Update () {	
}// END Update

function OnMouseDown ()
{
        // When you click over the object
        Debug.Log('Particle Activaction!');
        myParticles.SetActive(true); 
}// END OnMouseDown()

Inspector> DRAG AND DROP Particle System over var myParticles

Play and click over the Box Collider of Asteroid (EmptyObject).

For italian peolple: come funziona?

1. Creo un oggetto padre con all’interno la geometria e il sistema particellare
2. Resetto la posizione dei tre game object perchè coincidano
3. Creo un box collider nell’oggetto padre e assegno lo script
4. Lo script ottiene il sistema particellare come GameObject e lo attiva al click. Il sistema particellare parte di sicuro perchè ha attivo ‘Play On Awake’ in Inspector.