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 EwanKoSauHaha · Apr 19, 2014 at 07:43 AM · arraysruntime

enabling and disabling scripts in an array?

Usually, I do

blabla.GetComponent<"script">().enabled = true/false.

But there are many scripts I want to enable and disable at the same time. They are at different gameobjects so I just cant activate/deactivate the gameobject.

I tried to put it in a public array so I can just drag and drop the scripts I want to activate. But the problem is I cant enable or disable it.

public Component[] scripts;

public void Activate()

 {

     foreach (Component qwe in scripts)
     {

          qwe.enabled = true;//   <-- no ".enabled"
     }

 }


Is there a better way of activating a bunch of scripts together?

Comment
Add comment · Show 5
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 AlucardJay · Apr 19, 2014 at 07:59 AM 0
Share

typecast to the name of the class :

 public NameOfScriptHere[] scripts;
 
 public void Activate()
 {
     foreach (NameOfScriptHere qwe in scripts)
     {
          qwe.enabled = true;// <-- no ".enabled"
     }
 }
avatar image EwanKoSauHaha · Apr 19, 2014 at 08:07 AM 0
Share

I have different scripts like moveScript, healthScript etc. that I would like to enable and disable together.

avatar image AlucardJay · Apr 19, 2014 at 08:18 AM 0
Share

Sorry, that wasn't clear. I read as you had the same script on different gameObjects.

I can only suggest inheritance to make them all of the same base class, but there are much smarter people here.

avatar image EwanKoSauHaha · Apr 19, 2014 at 08:31 AM 1
Share

Thanks anyway. I found out the answer already. It should be $$anonymous$$onoBehaviour not Components

public Component[] scripts;

 // Update is called once per frame
 void Update () {
     foreach ($$anonymous$$onoBehaviour qwe in scripts)
     {
         qwe.enabled = false;
     }
avatar image Pulov · Mar 04, 2015 at 10:28 PM 0
Share

wow thanks I was so stuck with this. Certainly that $$anonymous$$onoBehaviour type was a gamechanger. thanks!.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Pulov · Mar 04, 2015 at 11:19 PM

This script will allow you people to switch betwen enable/disable the components you drag in the array. You can use it for many things as the input key is setable.

 using UnityEngine;
 using System.Collections;
 
 public class Components : MonoBehaviour {
 
     public Component [] components; //drag your scripts or whatever component you whant to disable here.
 
     private int index;
 
     public KeyCode ActivatorKey;// you can make a multipurpose script by using a editor inputed key, now it is not used change it bye Keycode.T.
 
 
 //---------------------------------------------------------------------------------------------------------
 
     void Start () {
         index = 0;
     }
     
 //---------------------------------------------------------------------------------------------------------
 
     void Update () {
         Component grayscale;
 
         if (Input.GetKeyDown(ActivatorKey)){
 
             foreach (MonoBehaviour qwe in components)
             {
                 qwe.enabled = !qwe.enabled;
             }
         }
     }
 }

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 vikas027 · Jan 11, 2018 at 05:30 AM 0
Share

thnx its workingg :)

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

24 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

Related Questions

Activating script by distance problem 3 Answers

Export objects to a .3DS file at runtime 1 Answer

Array of vectors lost in runtime, but not null 0 Answers

How to store gameobjects with specific scripts in a list ? 1 Answer

Can I make an array of scripts? 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