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.
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;
using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour {
void OnTriggerEnter(Collider other) {
other.gameObject.SetActive(false);
}
}
In this case, the game object that is triggering our trigger is the gameobject attached to the collider.
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.
You will need a public reference to it in your script. Thers a short video explaining it http://unity3d.com/learn/tutorials/modules/beginner/scripting/activating-gameobjects
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.
Read my post above.
But dont I have to name the object or something? how does it know that “other” is the object?
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.
![]()