Instantiate
Tutorial
·
Beginner
·
+10XP
·
5 mins
·
(1874)
Unity Technologies

How to use Instantiate to create clones of a Prefab during runtime.
This tutorial is included in the Beginner Scripting project.
Previous: Classes
Next: Arrays
Languages available:
1. Instantiate
UsingInstantiate
using UnityEngine;
using System.Collections;
public class UsingInstantiate : MonoBehaviour
{
public Rigidbody rocketPrefab;
public Transform barrelEnd;
void Update ()
{
if(Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * 5000);
}
}
}
RocketDestruction
using UnityEngine;
using System.Collections;
public class RocketDestruction : MonoBehaviour
{
void Start()
{
Destroy (gameObject, 1.5f);
}
}