# Make simple hit effect

I've made a game where you can shoot some cans, but I want to add an effect when a can is shot. You know, a kind of explosion or something like that. The way the game works now is: when the can is shot it disappears destroy(gameobject), but I want to add an effect before it disappears. Any suggestions? I want to keep it simple. The effect has to be in within the same method where I destroy the cans.

   public void Shot()
{
//Effect should be here
Destroy(gameObject);
}

• There are countless ways to do that. One you might want to look into is a particle effect. – Philipp Oct 24 '16 at 17:11
• Create an actual 3D animation that plays at the collision location. Treat it as an entity. – Krythic Oct 25 '16 at 13:37

For a 3D game, a simple particle effect spawned at the location of the can would be easy enough to do. For a 2D game, you could use a particle effect or a sprite animation at the point of impact. Heck, add a sound effect. Doing any of those could happen inside your Shot() function, ideally by calling a Subroutine. Just don't make the effect a child of the can, or it won't finish before the object is destroyed.

• Can you please show me how it's done in the method Shot()? I've tried to do it but can't make it work – user4476151 Oct 24 '16 at 17:23
• Since you're using Unity, I would create a particle effect that you like, create a prefab from it by dragging it from the project hierarchy to the Prefabs folder, then create a public ParticleSystem variable in the script, drag and drop the prefab into that slot in the inspector, then just Instantiate from that variable anytime hit is called. – Jesse Williams Oct 24 '16 at 17:30
• No prefabs are gameObjects, it must be public gameObject – UDXS Oct 24 '16 at 17:33
• Ah - this is true. I guess I minced my thoughts there. You could create an empty object, attach a particle system to it, put it in prefabs and do what I said... or, you can just build a particle system you like and drag and drop it from whatever location into the script if you use a ParticleSystem var. – Jesse Williams Oct 24 '16 at 17:35
• +1 for this is essentially the more abstract way to do what I said to do. – UDXS Oct 24 '16 at 17:38

I recommend you create a prefab with a particle system(or set of them) that also has a despawn timer script attached to it. The particles can be used for an explosion. The despawn script will wait a certain amount of time or until particle systems finish and will be attached to the top-level of the prefab. If you don't know about Prefabs, I suggest you research them because they are uber useful. You can use the Instantiate(Object theObject) function to spawn that prefab. You can make a public GameObject field in the MonoBehaviour of that Shot() method. This would allow you to simply drag the prefab from the Asset Explorer into the properties window for that object.

I hope this helped, if unclear, ask me to fix it.

• To be honest, I do understand it, but don't know how to do it. I do know what prefab is. How do I activate the particle system when can is hit? – user4476151 Oct 24 '16 at 17:26
• instanciate the prefab, if the particle system is configured correctly, it should run once. – UDXS Oct 24 '16 at 17:28
• Have done as you describe but now, when I shoot the cans I get this error: UnassignedReferenceException: The variable effect of Shotclass has not been assigned. You probably need to assign the effect variable of the Hit script in the inspector – user4476151 Oct 24 '16 at 20:32
• In addition to that, the particle system is also running – user4476151 Oct 24 '16 at 20:33

Create a particle on top of your script

public GameObject effect;


public void Shot()
{
//Effect should be here
Instanciate(effect, transform.position, transform.rotation);
Destroy(gameObject);
}


Then in the editor, create an empty gameobject containing you effect and drag it on your script.

• Have done as you describe but now, when I shoot the cans I get this error: UnassignedReferenceException: The variable effect of Shotclass has not been assigned. You probably need to assign the effect variable of the Hit script in the inspector – user4476151 Oct 24 '16 at 20:31
• In addition to that, the particle system is also running – user4476151 Oct 24 '16 at 20:33
• Have fixed it, but the particle system is running eve before I shoot a can – user4476151 Oct 24 '16 at 20:48
• Is the effect a child of the can ? If so it should not. – Ezkin Oct 25 '16 at 9:49

You should have a look at this SpaceShooter Tutorial from the Unity Website. Especially the Explosions Section https://unity3d.com/de/learn/tutorials/projects/space-shooter-tutorial/explosions?playlist=17147

You might want to tweak this particle Effect to your liking

Now all you need to do is to spawn this particle Effect before your bullet gets destroyed.

Beware to make sure that you destroy your Particle Effect after it has stopped.

You could do this like so:

using UnityEngine;
using System.Collections;

public class DestroyAfterStopped : MonoBehaviour {

private ParticleSystem myParticleSystem;

// Use this for initialization
void Start () {
myParticleSystem = GetComponent<ParticleSystem>();
}

// Update is called once per frame
void Update () {
if(myParticleSystem.isStopped)
{
if (gameObject != null)
{
Destroy(gameObject);
}
}
}
}


Just assign this Script to your ParticleSystem.