- Home /
Disabling Components C#
I'm trying to disable a component in C# that isn't attached to the current object the script is running on. I was using the below code to disable components attached to the current object, but how can I 'reach out' and disable others?
GetComponent<Camera>().enabled = false;
I know there is also GetComponentChildren but it's not a child object, it's a complete separate object, on it's own.
Thanks for any help provided!
Simply need references to thoses object and doing things through those references,like this: GameObject Enemy; Enemy.GetComponent().enabled = false;
Answer by ScottYann · Aug 03, 2013 at 11:40 PM
Enabling a component works just as you say:
GetComponent().enabled = false;
but if I read you right you want to do that to a different object. All you need then is the object reference. There are several ways of getting one.
You can use Find like this:
GameObject.Find("myDesiredObject").GetComponent().enabled = false;
This can work, however it is very costly in terms of cpu usage. You can get away with doing this if this line is only called once, once in a while like as the result of pressing a button. If you are calling this line in an Update or an IEnumerator the penalty is significant especially on mobile.
If you've preplaced your target object in your scene, why not just reference it through a public variable?
public GameObject myTarget
void OnMyEvent(){ myTarget.GetComponent().enabled = false }
Then if you haven't placed your target object in your scene and you've not done anything to reference it before hand, a sure fire way of getting to it with the smallest possible penalty is by getting to it via an object tag. Make a new tag, and assign your target object with that tag in it's prefab. Then do this to call it:
GameObject.FindGameObjectWithTag("myTag").GetComponent().enabled = false;
You can have dozens of tags and they make it easy to work with collections of objects. Say you had a smart bomb that blows up all your enemies. Just have a tag called "enemy" assign enemy objects that tag and do this:
foreach (GameObject enemy in GameObject.FindGameObjectsWithTag("enemy"){
Destroy(enemy); }
Answer by StormMuller · Dec 23, 2017 at 09:46 AM
So this is a really old post, but maybe I can help other people coming to this question.
The original poster's problem was that he needed to reference a gameObject other that the one the script is on. remember when using GetComponent<T>()
on it's own, you are referencing a member property from the MonoBehaviour
class, which knows what GameObject you are looking for(The Game object your script is attached to).
He could do this with any of the GameObject.Find
methods. or even better yet, create a public GameObject otherGameObject
then you can drag and drop another gameobject from the hierarchy or a prefab into the field in the inspector.
It's also important to note that the Component class does not have an enabled
property to toggle. This is because some components cannot be disabled(Like the transform component). However most components that can be disabled (Lights, Cameras, Colliders, etc.) are actually Behaviors
(Including your own custom MonoBehaviours) and Behaviors do have a enabled
property.
A common use case: You have a script that will turn certain components on for the local player in a networked game.
using UnityEngine;
using UnityEngine.Networking;
public class LocalPlayerEnabler : NetworkBehaviour
{
public Behaviour[] behavioursToTurnOn;
void Start()
{
if (!isLocalPlayer)
{
return;
}
foreach (var behaviour in behavioursToTurnOn)
{
behaviour.enabled = true;
}
}
}
Answer by Jamora · Jul 31, 2013 at 05:31 PM
You need a reference to the other GameObject. The easiest, but not the most efficient, is to use GameObject.Find with the name of the GamObject as a parameter. There are other ways to get a reference to other objects without using GameObject.Find, but I think you should look into those if you ever find your game's frame rates dropping.
I had come across that one, and as you stated can drop frame rate. You say that's the easiest, as if you know of another option? What other ways could this be accomplished? Thanks for your help so far!
Answer by Lovrenc · Jul 31, 2013 at 05:32 PM
You need a reference to other object.
otherObject.GetComponent<Camera>().enabled = false;
But this can get confusing spaghetti code really fast so be careful!
So something like:
CameraObject otherObject; otherObject.GetComponent().enabled = false;
Cause I tried something similar to that, the issue is it's multiplayer, and the object isn't created until the player is in-game, and it puts the objects in place, so when I try to reference it, I get an error that it doesn't exist. Thanks for the help so far!
$$anonymous$$ight be a little late. But try putting it in an if statement to check if the object is created.
if (otherObject != null) {
otherObject.GetComponent().enabled = false;
}
Answer by eaglemaster7 · Jul 31, 2013 at 05:32 PM
try using GameObject.Find("Your object name").GetComponent().enabled = false;
Thanks for the reply! That was one option I came across, but I kept seeing comments about it eating a lot of resources. I had this same problem on a 'test' project, and had a working disable script, but for the life of me can't remember what I did. It was like 2 lines per component, any ideas?
Your answer
![](https://koobas.hobune.stream/wayback/20220613111301im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GetComponentsInChildren Network Reliable 0 Answers
Help removing an AudioSource component from another script 0 Answers
Only one Child Component turning off. 2 Answers