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