Instantiate

確認済のバージョン: 4

-

難易度: 初級

Instantiate を使ってランタイムにプレハブのクローンを作成する方法を学びます。

Instantiate

初級 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)