0
\$\begingroup\$

Have a game where you've to shoot somethings and when you hit the GameObject I've should play. I've tried with Instantiate but I can keep shooting even if the gameobject is destroyed. What should I do in order to fix it?

 public GameObject particlesystem;

 void Start () {
    particlesystem= GameObject.Find("Effect");
 }

 void Hit(){
 Destroy(gameobject)
 Instantiate(particlesystem, transform.position, transform.rotation);
 }

This is how I handle it now, but as I wrote earlier I can keep activating the particle system.

\$\endgroup\$

2 Answers 2

1
\$\begingroup\$

Particle System

Get your particle system in code and use public methods like Play() and Stop().

Just as an example of use:

this._particleSystems[i].Play();
if (Time.time > 50) {
    this._particleSystems[i].Stop();
}
\$\endgroup\$
-1
\$\begingroup\$

I think that the best way to do it is, instead of

particlesystem= GameObject.Find("Effect");

assign the prefab in the unity GUI (in the script field)

In addition, when you want to play the effect you should have to do it this way

var gO = Instantiate(particlesystem, transform.position, transform.rotation);
Destroy(gO, seconds);

I don't remember if var is a gameobject or an object type, but with var it should work.

Edit: if you have to destroy the game object (which are you shooting) call destroy(gameobject) too

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .