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);
}
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With