Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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 this script will disable all other scripts on a gameobject until the right conditions are met, in this place the conditions are stored in a playerpref, so long as the playerpref != 1 I want all scripts (excluding this 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 != this.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 != this.name) 
                         {
                             c.enabled = false;
                         }
                     }
 

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

Comment
Add comment
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
1
Best Answer

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

The name property in MonoBehaviour returns the name of the GameObject to which 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 something like this:

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

The reason this works is due to a property of C# called polymorphism. 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 (which happens occasionally, depending on design) this is a pretty powerful tool to add to your toolbox.

If you're not comfortable with polymorphism, 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 thing, 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 think). Additionally, I think you also pay a performance hit 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
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
0

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

 public MonoBehaviour[] disabledOnes;
  public void Update(){
  if (disabledOnes.Length > 0)
      {
          foreach (MonoBehaviour disableThis in disabledOnes)
          {
              disableThis.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 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.

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

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

How do I show a custom dynamic string instead of the script name of a MonoBehaviour component in the Inspector? 1 Answer

Disabling GameObject doesn't disable components 0 Answers

I kill all myenemies instead of one 1 Answer

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