Instantiate
Geprüft mit Version: 4
-
Schwierigkeitsgrad: Anfänger
How to use Instantiate to create clones of a Prefab during runtime.


Instantiate
Anfänger Scripting
UsingInstantiate
Code snippet
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);
}
}
}
#pragma strict
public var rocketPrefab : Rigidbody;
public var barrelEnd : Transform;
function Update ()
{
if(Input.GetButtonDown("Fire1"))
{
var rocketInstance : Rigidbody;
rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
rocketInstance.AddForce(barrelEnd.forward * 5000);
}
}
import UnityEngine
import System.Collections
public class UsingInstantiate(MonoBehaviour):
public rocketPrefab as Rigidbody
public barrelEnd as Transform
private def Update():
if Input.GetButtonDown('Fire1'):
rocketInstance as Rigidbody
rocketInstance = (Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation) as Rigidbody)
rocketInstance.AddForce((barrelEnd.forward * 5000))
RocketDestruction
Code snippet
using UnityEngine;
using System.Collections;
public class RocketDestruction : MonoBehaviour
{
void Start()
{
Destroy (gameObject, 1.5f);
}
}
#pragma strict
function Start ()
{
Destroy (gameObject, 1.5f);
}
import UnityEngine
import System.Collections
public class RocketDestruction(MonoBehaviour):
private def Start():
Destroy(gameObject, 1.5F)
Zugehörige Dokumentation
- Script Reference - Instantiate (Skriptreferenz)
- Manual - Prefabs (Benutzerhandbuch)