Instantiating & Destroying Gameobjects in Unity

by Lance Gold

Each added GameObject can have a Script to create some behavior in the game

Return to index

Here we add a script to a capsule named Laser so that it moves up after instantiation

Right click to create a new C# Script

Right click to create a new C# Script

Add the script as a component to the Prefabs Laser object:

(a) highlight Prefabs Laser

(b) drag and drop Scripts Laser onto Prefabs Laser.

Drag Scripts Laser onto Prefabs Laser

Drag Scripts Laser onto Prefabs Laser

With Laser highlighted, check the Inspector to see the added Script component

Laser Script appears as added Component

Laser Script appears as added Component

Now double click to switch to C sharp coding in the Laser.cs tab of Visual Studio. Each tab is a scripting class: the Laser.cs, the Player.cs. The classes Laser and Player inherit from the UnityEngine MonoBehaviour class.


using UnityEngine;

public class Laser : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

A search from Help to look for Transform.Translate()

Help option to search Scripting Reference

Help option to search Scripting Reference

After a search:

Transform.Translate

Declaration

public void Translate(Vector3 translation);


using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    void Update()
    {
        // Move the object forward along its z axis 1 unit/second.
        transform.Translate(Vector3.forward * Time.deltaTime);

        // Move the object upward in world space 1 unit/second.
        transform.Translate(Vector3.up * Time.deltaTime, Space.World);
    }
}

So for our vertical example, “up”:


    [SerializeField]
    private float _speed = 8f;

    // Update is called once per frame
    void Update()
    {
        transform.Translate(Vector3.up * _speed * Time.deltaTime);
    }

The lasers rise out of the camera view

Example of laser objects up

Example of laser objects up

To find the top of the view, run game, shoot and pause to mouse move a laser object. Leave Console view and click Game view tab.

Paused Game view showing laser at top limit

Paused Game view showing laser at top limit

Looking at the Transform position, we can pick a max height to delete, destroy the laser object.

Transform position of laser at Y = 4.72

Transform position of laser at Y = 4.72


public class Laser : MonoBehaviour
{
    [SerializeField]
    private float _outOfView = 7.0f;

    void Update()
    {
        // if higher than _outOfView destroy laser !! up is positive
        if (transform.position.y > _outOfView)
        {
            Destroy(gameObject);
        }
    }

“Destroy(this)” applies only to the script, not the gameObject, so most descriptive is “Destroy(this.gameObject);”


              Destroy(this.gameObject);

From the Scripting API, there is a time delay available also:

Object.Destroy

Declaration

public static void Destroy(Object obj, float t = 0.0F);