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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Make GameObject disappear?

Discussion in 'Scripting' started by Treasureman, Jan 21, 2016.

  1. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I have a GameObject in my scene that I want to be disabled when a certain onTrigger collider touches it. How would I do this?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,744
    You can disable the renderer or deactivate the GameObject.
    Code (csharp):
    1. void OnTriggerEnter(Collider other) {
    2. other.gameObject.SetActive(false);
    3. //or
    4. other.GetComponent<Renderer>().enabled = false;
    5. }
     
  3. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
    I don't get any errors or anything, but it just doesn't do anything. I have a pretty novice understanding of C# so tell me if there is anything I did wrong. I want it so that the sword collider will disable the player variable...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Death : MonoBehaviour {
    5.  
    6.     public GameObject sword;
    7.     public GameObject player;
    8.  
    9.     void OnTriggerEnter(Collider sword) {
    10.         player.gameObject.SetActive(false);
    11.         //or
    12.         player.GetComponent<Renderer>().enabled = false;
    13.     }
    14. }
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356

    Remove the Public GameObject sword, there's no need for that.

    That script should work, you need to make sure one of the objects that's doing the colliding has a rigidbody attached to it. OnTriggerEnter won't be sent unless one of them is a rigidbody.
     
  5. Treasureman

    Treasureman

    Joined:
    Jul 5, 2014
    Posts:
    563
     
  6. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    No problem!