Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

collider other.gameObject

Discussion in 'Scripting' started by Hyobin_Kim, Jun 25, 2018.

  1. Hyobin_Kim

    Hyobin_Kim

    Joined:
    Feb 25, 2018
    Posts:
    17
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DestroyByBoundary : MonoBehaviour
    6. {
    7.     void OnTriggerExit(Collider other)
    8.     {
    9.         Destroy (other.gameObject);
    10.     }
    11.  
    12. }
    This is a very simple code for boundary setting for my game

    while practicing with this code

    I got to have a question

    for the first time I put like this "Destroy(other)"

    What I had in my mind was there "other" means

    any other colliders leaving the collider where this script is attached

    of course the object I created as a laser bolt has its "Capsule Collider"

    So I expected just with "other" inside "Destroy()" function

    but It was not working correctly

    why do i need to specify "gameObject" there?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Destroy(other) will just destroy the Collider you hit but nothing else.

    Destroy(other.gameObject) will destroy the entire GameObject containing the collider you hit, and everything parented beneath it.
     
    Hyobin_Kim likes this.
  3. Hyobin_Kim

    Hyobin_Kim

    Joined:
    Feb 25, 2018
    Posts:
    17
    Thank you so much for your detailed explanation
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You're welcome! It's helpful to think about Unity's organization of stuff: every single entry in your Hierarchy window is a GameObject, and Components are plugged into GameObjects.

    Every GameObject has a Transform component, plus whatever other Components you put on it: Camera, Collider, Renderer, Light, your own scripts (Monobehaviors) etc.

    And finally Components can have often Asset references plugged into them: Materials, Textures, Models, or references to other Components or GameObjects.