Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destroying particle systems

In my game there is a ball hits a coin then the coin disappeared and a particle system is initialized.

my problem is to destroy the particle system i tried to write

Destroy(effect.gameObject)

but i got an error message tell me that there is no definition for gameObject.

my unity version is 4.6.3

Help will be appreciated.

this is my code

public class CoinDestroyer : MonoBehaviour {
   public Transform coinEffect;
   void OnTriggerEnter (Collider other){
       if (other.tag == "Player"){
           var effect = Instantiate(coinEffect, transform.position, transform.rotation);
           Destroy(effect.gameObject, 3);
           Destroy(gameObject);
       }
   }
}
like image 848
Mahmoud Anwer Avatar asked Aug 09 '26 17:08

Mahmoud Anwer


1 Answers

Instantiate returns an object of type Object, the top class in Unity (not the .NET type). Since you use var effect, the compiler is fine making effect an Object. But you need a GameObject since Object has no gameObject member.

var effect = (GameObject)Instantiate(coinEffect, transform.position, transform.rotation);

This is one of the danger of using var instead of strongly type variables. Best would be:

GameObject effect = (GameObject)Instantiate(coinEffect, transform.position, transform.rotation);

In this case, if the cast is missing the compiler will throw an error complaining that Object cannot be GameObject and you need a cast.

I only use var in cases I am 100% sure of the type and it is a long one to write like KeyValuePair<string,List<GameObject>>, else, always the right type.

like image 158
Everts Avatar answered Aug 11 '26 06:08

Everts