You can instantiate prefabsAn asset type that allows you to store a GameObject complete with components and properties. The prefab acts as a template from which you can create new object instances in the scene. More info
See in Glossary 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 GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary which acts as a launcher for the prefab.
Projectile
and then drag it into the Assets
folder of your project to create a prefab asset.You can optionally add a textureAn image used when rendering a GameObject, Sprite, or UI element. Textures are often applied to the surface of a mesh to give it visual detail. More info
See in Glossary to the prefab, change its dimensions, or import a different modelA 3D model representation of an object, such as a character, a building, or a piece of furniture. More info
See in Glossary to act as the projectile.
To add an explosion to the projectile prefab, you must have a prefab asset that represents an explosion. You can use the particle systemA component that simulates fluid entities such as liquids, clouds and flames by generating and animating large numbers of small 2D images in the scene. More info
See in Glossary to create a prefab asset, or find an explosion effect on the Asset StoreA growing library of free and commercial assets created by Unity and members of the community. Offers a wide variety of assets, from textures, models and animations to whole project examples, tutorials and Editor extensions. More info
See in Glossary and add it to the Assets
folder of your project.
Then create a script called Projectile
as follows:
using 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:
Projectile
prefab asset and open it in prefab editing mode.Projectile
script onto it.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 FireProjectile
and add the following contents to it:
using 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 Instantiate
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).
For component type variables (such as Rigidbody, Collider, and Light), you can only assign GameObjects of that component type to the variable, and the Instantiate
function returns a reference to that specific component on the new GameObject instance.
You must attach the script to a GameObject to use it. To do so:
FireProjectile
script onto the cube.Projectile
prefab asset into the Projectile field of the Fire Projectile script.The cube fires the sphere projectiles and the explosion happens when they collide with the ground.
Note that any instantiated objects appear in the Hierarchy with (Clone)
appended to the name.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.