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?
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?
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.
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.
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.
Your answer

Follow this Question
Related Questions
InvalidOperationException when peeking state in custom game editor 0 Answers
How to pass the variable of a current state machine behavior 0 Answers
can't add script behaviour TMP_SelectionCaret . The script need to derive from MonoBehaviour 4 Answers
Why does unity not see monobehaviour? 2 Answers
How to enable only one script (shared by multiple gameobjects) at a time/click ? 1 Answer