- Home /
How to find a component and disable it from another objects script?
Hey,
so how could I disable a Camera on an object when the script which disables it is in another object.
#pragma strict
var failSound : AudioClip;
function OnTriggerEnter(col : Collider)
{
if (col.tag == "Player")
{
audio.PlayOneShot(failSound);
print("Dead");
GetComponent(Camera).enabled = false;
}
else
{
GetComponent(Camera).enabled = true;
}
}
Answer by getyour411 · Sep 20, 2013 at 09:42 PM
Like this
I'm not trying to access a script on another object but a component like a camera.
This is an answer section, not a "link-to-google-search" section.
Answer by Dragate · Nov 16, 2017 at 12:25 PM
You can search for components with
FindObjectOfType() (if you know there's only one of that type, so it'll return that)
FindObjectsOfType() (to get multiple components of that type and search through them)
Note: Like GameObject.Find(), it is an expensive function.
Specifically, for main or any other (check the static properties in the docs linked) camera you could easily disable like this:
Camera.main.enabled = false;
This is way better (performance-wise) than FindObjectOfType() assuming you're interested in camera components only.
Your answer
Follow this Question
Related Questions
disable/enable script js 2 Answers
Unable to enable script 0 Answers
Problem disabling a script 1 Answer
enable/disable specific components 3 Answers