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. Dismiss Notice

accessing gameobjects in C#

Discussion in 'Scripting' started by mikuru15, Jan 2, 2015.

  1. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    Noob here
    How do access a gameobject in a script that isnt attached to that object?
    I want to disable a gameobject when a collider is triggered.
     
  2. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,121
    If your script is on the object with the trigger, and you want to disable the object that is triggering it, then you can do the following;

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void OnTriggerEnter(Collider other) {
    6.         other.gameObject.SetActive(false);
    7.     }
    8. }
    In this case, the game object that is triggering our trigger is the gameobject attached to the collider.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    Which gameobject do you want to disable?

    Is it the gameobject on which the collider is attached that entered the trigger? Prodigga's example would work.

    Is it just some other gameobject in the world? There's all sorts of ways to access that, use the 'Find' command, have a variable on the script that you attach a reference to what gameobject you want to disable, and many more.

    Is it some parent of the collider that entered the trigger? This one is a bit more complicated because you don't necessarily know the hierarchy of the gameobjects that the collider is attached to. If you want the rigidbody the collider works with, just search up the parent chain for it. Personally what I do is a tag the 'root' gameobject of any 'entity' (collection of gameobjects that should be considered a single entity) and I search up the parent chain for that tag.

    Is it some other scenario I did not describe?

    A bit of advise from a professional with nearly a decade of experience. One of the crucial things about learning programming is being able to describe EXACTLY what it is you want to do. A computer can only do that which you tell it.
     
    Last edited: Jan 2, 2015
    Kiwasi and Prodigga like this.
  4. REV-FX

    REV-FX

    Joined:
    Oct 24, 2013
    Posts:
    28
  5. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    The gameobject I want to disable is not attached to the trigger, its not a child object. the trigger collider is a whole other object which the script will be attached to. when it is triggered I wanted to call forth that other object in my game and have it disabled. What I dont know is how to call out to that object.
     
  6. Prodigga

    Prodigga

    Joined:
    Apr 13, 2011
    Posts:
    1,121
    Read my post above.
     
  7. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    But dont I have to name the object or something? how does it know that "other" is the object?
     
  8. mikuru15

    mikuru15

    Joined:
    Nov 17, 2014
    Posts:
    17
    Awesome I figured it out, just looked up the find command.
    Thanks for pointing me in the right direction guys, im thankful there are people willing to take the time to help.
    :)
     
    Prodigga likes this.