Destroy

確認済のバージョン: 4

-

難易度: 初級

ランタイムにゲームオブジェクトとコンポーネントを削除する Destroy() 関数の使い方を学びます。

Destroy

初級 Scripting

DestroyBasic

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyBasic : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            Destroy(gameObject);
        }
    }
}
#pragma strict

function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(gameObject);
    }
}
import UnityEngine
import System.Collections


public class DestroyBasic(MonoBehaviour):

    private def Update():
        if Input.GetKey(KeyCode.Space):
            Destroy(gameObject)

DestroyOther

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyOther : MonoBehaviour
{
    public GameObject other;
    
    
    void Update ()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            Destroy(other);
        }
    }
}
#pragma strict

public var other : GameObject;


function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(other);
    }
}
import UnityEngine
import System.Collections


public class DestroyOther(MonoBehaviour):

    public other as GameObject

    
    
    private def Update():
        if Input.GetKey(KeyCode.Space):
            Destroy(other)

DestroyComponent

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyComponent : MonoBehaviour
{
    void Update ()
    {
        if(Input.GetKey(KeyCode.Space))
        {
            Destroy(GetComponent<MeshRenderer>());
        }
    }
}
#pragma strict

function Update ()
{
    if(Input.GetKey(KeyCode.Space))
    {
        Destroy(GetComponent(MeshRenderer));
    }
}
import UnityEngine
import System.Collections


public class DestroyComponent(MonoBehaviour):

    private def Update():
        if Input.GetKey(KeyCode.Space):
            Destroy(GetComponent[of MeshRenderer]())