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
0
Question by theUndeadEmo · Apr 14, 2012 at 09:33 AM · arraydisable

how to disable specific scripts in an array of scripts

hello, i would like to know how to disable specific scripts in an array of scripts, at the moment it disables all

 private MonoBehaviour [] scripts;
 // Use this for initialization
 void Start ()
 {

     scripts =GetComponents<MonoBehaviour>() ;
     if (Application.loadedLevel == 0) {
         foreach (MonoBehaviour s in scripts) {
             if (s.name!="EnableScripts"){
                 s.enabled=false;
             }
         
         }
         
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by gregzo · Apr 14, 2012 at 09:57 AM

You need to check the type of the script, like this:

 if(typeof(s)!=EnableScripts)
 {
     s.enabled = false;
 }

This would disable a script if it is of type EnableScripts. Don't forget a script is a class, and in UnityScript, a script's name is it's type! No need for quotes...

Edit: Also, print(s.name); would give you the name of the gameObject the script is attached to, not the script's!

Comment
Add comment · Show 4 · 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 Kleptomaniac · Apr 14, 2012 at 10:01 AM 0
Share

Good answer!

avatar image theUndeadEmo · Apr 14, 2012 at 10:28 AM 0
Share

error CS0246: The type or namespace name `s' could not be found. Are you missing a using directive or an assembly reference?

getting that error :(

avatar image fafase · Apr 14, 2012 at 10:32 AM 0
Share

Did you put it inside the foreach?

foreach ($$anonymous$$onoBehaviour s in scripts) { if(typeof(s)!=EnableScripts) { s.enabled = false; } }

avatar image theUndeadEmo · Apr 14, 2012 at 10:35 AM 0
Share

foreach ($$anonymous$$onoBehaviour s in scripts) {

             if(typeof(s)!=EnableScripts){
                 s.enabled=false;
             }
         
         }
avatar image
0

Answer by theUndeadEmo · Apr 14, 2012 at 10:57 AM

ok i got it working, thanks to the hint from gregzo

 if(s!=GetComponent <EnableScripts>()&& s!=GetComponent <racePoints>()){
                 s.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 gregzo · Apr 14, 2012 at 11:01 AM 0
Share

Hmm, could be much simpler I$$anonymous$$O, see my answer below, but it's in UnityScript, not C#...

avatar image
0

Answer by gregzo · Apr 14, 2012 at 10:59 AM

     scripts = GetComponents(MonoBehaviour);
     
     for(s in scripts) 
     {
        if (typeof(s)!=EnableScripts)
        {
             s.enabled=false;
        }
     }

This works fine (in UnityScript).

Comment
Add comment · Show 3 · 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 theUndeadEmo · Apr 14, 2012 at 11:04 AM 0
Share

not sure why it doesn't work for me :( (using unity 3.5 beta)

avatar image gregzo · Apr 14, 2012 at 11:06 AM 0
Share

why still the beta? Upgrading doesn't have many downsides! And sorry I can't help you with C# syntax for this. Hopefuly, a sharper coder will chime in!

avatar image theUndeadEmo · Apr 14, 2012 at 11:20 AM 0
Share

just typed unity 3.5 and it took me to the beta page, but anyway i did manage to get it working but in a different way

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Get all Scripts Attached to gameObject 1 Answer

How to pass array variables from one script in one scene to another script in another scene? 2 Answers

Array script boolean 1 Answer

how would I make an array of different scripts 1 Answer

Adding an array of scripts to a class 0 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