Instantiate projectiles and explosions
Use prefabs to instantiate exploding projectiles.
Read time 4 minutesLast updated 21 days ago
You can instantiate prefabs to use as projectiles and destroy them with explosion effects in your application.
The following example instantiates a projectile prefab when the user presses the fire button. You can attach it to a GameObject which acts as a launcher for the prefab.
Create a projectile prefab asset
- In the Hierarchy, right-click and select > Sphere.3D Object
?
- Select the sphere, and in its Inspector, select Add Component > . The sphere needs a Rigidbody so that it can fly through the air and detect when a collision happens.Rigidbody
?
- Rename the sphere to and then drag it into the
Projectilefolder of your project to create a prefab asset.Assets - You can then delete the sphere from the Hierarchy.
You can optionally add a texture to the prefab, change its dimensions, or import a different model to act as the projectile.
Add an explosion script to the projectile prefab asset
To add an explosion to the projectile prefab, you must have a prefab asset that represents an explosion. You can use the particle system to create a prefab asset, or find an explosion effect on the Asset Store and add it to the folder of your project.
AssetsThen create a script called as follows:
Projectileusing UnityEngine;public class Projectile : MonoBehaviour{ public GameObject explosion; void Start() { // Deletes the projectile after 10 seconds, regardless // of whether it collided with anything. This prevents // instances from staying in the scene indefinitely. Destroy(gameObject,10); } void OnCollisionEnter() { // When the projectile hits something, create an explosion // and remove the projectile. Instantiate(explosion,transform.position,transform.rotation); Destroy(gameObject); }}
The script instantiates the explosion at the projectile's current position and removes the projectile GameObject when the projectile collides with something.
To use the script, attach it to the projectile prefab asset:
- Select the prefab asset and open it in prefab editing mode.
Projectile - In the Inspector, drag the script onto it.
Projectile - Drag the explosion prefab asset into the Explosion field.
Create a script to launch the projectiles
To launch the projectiles, you need to create a script that instantiates projectiles when the fire key is pressed, and add that script to a GameObject.
Create a script called and add the following contents to it:
FireProjectileusing UnityEngine;using UnityEngine.InputSystem;public class FireProjectile : MonoBehaviour{ // Only GameObjects with a Rigidbody can be assigned as the projectile. public Rigidbody projectile; // Speed of the projectile when fired. // This is a public variable so it can be adjusted in the Unity Editor. public float speed = 4; // Update is called once per frame // This method checks for input and fires a projectile if the attack action is pressed. void Update() { // Check if the "Attack" action was pressed this frame // If it was, instantiate a projectile at the player's position and set its velocity. if (InputSystem.actions.FindAction("Attack").WasPressedThisFrame()) { Rigidbody p = Instantiate(projectile, transform.position, transform.rotation); p.linearVelocity = transform.forward * speed; } }}
This script uses to launch a projectile. When making a public prefab variable, the variable type can be a GameObject, or it can be any valid component type (either a built-in Unity component or one of your own MonoBehaviour scripts).
InstantiateFor component type variables (such as Rigidbody, Collider, and Light), you can only assign GameObjects of that component type to the variable, and the function returns a reference to that specific component on the new GameObject instance.
InstantiateAttach the launcher script to a GameObject
You must attach the script to a GameObject to use it. To do so:
- Make sure that your scene has a ground GameObject for the projectile to collide with. If you're using an empty project, create a Plane GameObject: right-click on the Hierarchy and select 3D Object > Plane.
- Create a Cube GameObject: right-click on the Hierarchy and select 3D Object > Cube and position it over the plane.
- Select the cube, and in its Inspector, delete the component. If you don't delete this component, the projectiles collide with the cube before hitting the ground.Box Collider
?
- Drag the script onto the cube.
FireProjectile - Drag the prefab asset into the Projectile field of the Fire Projectile script.
Projectile - Enter Play mode, and then click your mouse button.
The cube fires the sphere projectiles and the explosion happens when they collide with the ground.

Projectile and explosion prefabs being instantiated and destroyed.
Note that any instantiated objects appear in the Hierarchy with appended to the name.
(Clone)