• 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
Question by Razputin · Jul 01, 2017 at 06:02 AM · script.monobehaviourforeachnameenabled

Disable all scripts on gameobject excluding the one calling for disable

Trying to make it so t$$anonymous$$s script will disable all other scripts on a gameobject until the right conditions are met, in t$$anonymous$$s place the conditions are stored in a playerpref, so long as the playerpref != 1 I want all scripts (excluding t$$anonymous$$s one) to be disabled.

     public string playerPrefName;
 
     void Start ()
     {
         if(playerPrefName == null)
         {
             Debug.Log("Didn't Specify PlayePref");
         }
     }
     
     void Update ()
     {
         if (playerPrefName != null)
         {
             if (PlayerPrefs.GetInt(playerPrefName) == 0)
             {
                 MonoBehaviour[] comps = GetComponents<MonoBehaviour>();
                 foreach (MonoBehaviour c in comps)
                 {
                     if (c.name != t$$anonymous$$s.name) 
                     {
                         c.enabled = false;
                     }
                 }
             }
             else
             {
                 MonoBehaviour[] comps = GetComponents<MonoBehaviour>();
                 foreach (MonoBehaviour c in comps)
                 {
                     c.enabled = true;
                 }
             }
         }
     }
 }
 

The problem comes from here

                     foreach (MonoBehaviour c in comps)
                     {
                         if (c.name != t$$anonymous$$s.name) 
                         {
                             c.enabled = false;
                         }
                     }
 

I tried doing if (c.name != t$$anonymous$$s.name) to stop the script calling itself but it seems like saying "t$$anonymous$$s.name" calls the name of the current c and not the name of the script therefore not$$anonymous$$ng gets disabled.

Comment
SepM

People who like this

1 Show 0
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

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Vicarian · Jul 01, 2017 at 06:52 AM

The name property in MonoBehaviour returns the name of the GameObject to w$$anonymous$$ch a Behaviour is attached. You'd have to hold a reference to the disabler script or check its type as you loop over the other Components. So, assuming the type of your MonoBehaviour is called Disabler, you'd do somet$$anonymous$$ng like t$$anonymous$$s:

 foreach (MonoBehaviour c in comps)
 {
     if (c.GetType() != typeof(Disabler)) 
     {
         c.enabled = false;
     }
 }

The reason t$$anonymous$$s works is due to a property of C# called polymorp$$anonymous$$sm. You may treat derived classes as their base class and just use a differentiator (a subtype) to take specific actions against that type. Similarly, an initializer:

 MonoBehaviour m = new Disabler(); 

works in base C#, but not in Unity due to constraints on MonoBehaviour constructors imposed by the Engine. However, if you use vanilla classes in your games (w$$anonymous$$ch happens occasionally, depending on design) t$$anonymous$$s is a pretty powerful tool to add to your toolbox.

If you're not comfortable with polymorp$$anonymous$$sm, you can use the aforementioned reference route, like so:

 Disabler d = GetComponent<Disabler>();
 
 foreach (MonoBehaviour c in comps)
 {
     if (c != d) 
     {
         c.enabled = false;
     }
 }

Accomplishes the same t$$anonymous$$ng, but at the small expense of a small amount of memory getting allocated to hold a reference to the type (around 64 bits for a pointer, I t$$anonymous$$nk). Additionally, I t$$anonymous$$nk you also pay a performance $$anonymous$$t because I'm fairly certain GetComponent uses Reflection to differentiate types. It's a hunch, so don't quote me on that. I'm not a Unity engineer.

Comment
SepM

People who like this

1 Show 0 · 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

Answer by omerselman · Jul 01, 2017 at 08:57 AM

 public MonoBehaviour[] disabledOnes;
  public void Update(){
  if (disabledOnes.Length > 0)
      {
          foreach (MonoBehaviour disableT$$anonymous$$s in disabledOnes)
          {
              disableT$$anonymous$$s.enabled = false;
          }
      }
 }
 
Comment

People who like this

0 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 omerselman · Jul 01, 2017 at 09:01 AM 0
Share

You may use a button Or a function rather then update

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 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 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 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

How to add a pre-existing MonoBehaviour component object (stored in a variable set at runtime) to an pre-existing game object 1 Answer

Is There a List of MonoBehaviour Messages That Are Still Called When Disabled? 1 Answer

Disabling GameObject doesn't disable components 0 Answers

Toggle script with button 0 Answers

UnityEngine.Object is null but System.Object is NOT null! 3 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