Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by umurcg · Oct 18, 2016 at 01:39 PM · monobehaviourenable and disable script

Can I enable and disable specific scripts of all objects that having this script without finding these objects?

Hello,

Let say I have a specific script called "Click". And lots of objects of scene have this scripts.

Can I disable and enable "Click" script of all those object without finding those objects.

I managed to disable them with using static boolean variable. I added a if condition in Update function that checks static disabeled boolean. However I can't enable these scripts whit setting disabled boolean to false after I disabled them while scripts doesn't call Update method when they are not enabled.

Are there any other way to do that without costing too much performance?

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 TreyH · Oct 18, 2016 at 01:48 PM 0
Share

What's wrong with your solution? If that static value is public, couldn't you just toggle it by calling and setting the class' variable itself?

avatar image umurcg TreyH · Oct 18, 2016 at 03:14 PM 0
Share

I can change static value. But script doesn't call Update function when it is not enabled. So it can't enable itself while it is disabled.

As I said I can disable scripts but can't enable them.

avatar image TreyH umurcg · Oct 18, 2016 at 03:25 PM 0
Share

Oh, maybe I misread. I assumed you had something like:

 public static bool manualDisable = false;
 
 void Update()
 {
     if (manualDisable)
          return;
     
     // Rest of update ...
 }

You could still edit that value (Script.manualDisable = true;) from anywhere.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Diukrone · May 10, 2020 at 03:13 AM

You need to delegate an event Mr. @umurcg

Create new a C# script. Name it EnableDisableSystem.cs

After it, create a delegate and an event and it´s method:

 public delegate void EnableAndDisable();
 public static event EnableAndDisable ScriptsTarget;

 public bool isItEnabled = true;

 // Start is called before the first frame update
 public void Update()
 {
         if (ScriptsTarget != null)
         {
            ScriptsTarge();
         }
 }

go to your Click.cs Start method and put the += and -= calls for your event.

 public void Start()
 {
     EnableAndDisable.ScriptsTarget += EnableAndDisableScripts;
 }

 public void EnableAndDisableScripts()
 {
      Debug.Log("Your code here!");
 }

After the code to be done your function, unregister the script´s method from the event´s declaration.

 public void OnDisable()
 {
     EnableAndDisable.ScriptsTarget -+= EnableAndDisableScripts;
 }

Now in EnableDisableSystem put a condition on it´s method that will send a call to all scripts to turn off or turn on, In the case it will stop to send data to event after gets disable but will can receive external orders to wake up.

If you liked my answer, please vote up! Thanks!

Will be simultaneous to all scripts and will be a bit optimized. Learn with struct when you to can to help these delegations and events data moving! Jobs and ECS are welcome on this way i do.

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

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

77 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inheriting from a class that inherits from monobehaviour 3 Answers

How to inherit from other class in c#? 1 Answer

How do I use GetComponent to populate and array? 1 Answer

After 2019.1.3 upgrade scripts aren't working properly the snippets not showing intelisense parameter info 0 Answers

Call method of a behaviour from a scripotableobject 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