Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by VirtualJunky · Jul 31, 2013 at 05:21 PM · c#componentdisabling

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!

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nixcs2512 · Jul 31, 2013 at 05:39 PM 0
Share

Simply need references to thoses object and doing things through those references,like this: GameObject Enemy; Enemy.GetComponent().enabled = false;

5 Replies

· Add your reply
  • Sort: 
avatar image
4

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); }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
3

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;
         }
     }
 }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VirtualJunky · Jul 31, 2013 at 09:03 PM 0
Share

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!

avatar image
0

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VirtualJunky · Jul 31, 2013 at 09:05 PM 0
Share

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!

avatar image sjos VirtualJunky · Jan 21, 2021 at 07:28 AM 0
Share

$$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;
 }

avatar image
0

Answer by eaglemaster7 · Jul 31, 2013 at 05:32 PM

try using GameObject.Find("Your object name").GetComponent().enabled = false;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VirtualJunky · Jul 31, 2013 at 09:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges