How to set object behaviour on variable change?

Hi,
I can’t find clean and efficient way to e.g. gameobject.SetActive(false) on static variable change in another script. First script:
void ChangeVar() { var1 = true; }
in second script I want to check if var1 is true and set gameobject as inactive (just once!)
Thank you all :slight_smile:

@Bartson, I first note that the name ChangeVar and var1 have little meaning. They don’t say anything about what they do. Since your stated objective is to set a gameObject to inactive, it seems to be a disable command of some kind. In some weeks or months later, when one returns to see ChangeVar is a function name, it will be difficult, without reading through code, to remember what it is really doing.

It is difficult to recognize why one script would set another object inactive if there were only 1 script/object to deactivate. I would assume the object to be deactivated would do so to itself, but perhaps the first script, whatever it is, is in the primary position to sense when to deactivate the other object. It is more likely that there will be multiple gameObjects that qualify for deactivation under some circumstance, which would imply an executive that drives the process.

The relationship suggested by your description is that of a supervisor and a subordinate, or perhaps a parent/child. I’ll refer to the object(s) to be deactivated as the child subsequently.

If ChangeVar is indeed a deactivation function, controlled by a static member, then it may be best for it to perform the deactivation, and not the child at some later moment. I have to admit there may be reasons for the child to recognize this state and deactivate after performing some kind of shutdown operation, but it isn’t the most likely requirement.

Is there not a need to activate the object(s) later? If the bool var1 is actually an indicator of active/deactive state, there should be a method to reactivate the object(s) when this is set back to true.

I have to assume a name for the class that owns the static var1 and the ChangeVar function, so I’ll assign Supervisor. If var1 is a public static bool, it is available to all other scripts as Supervisor.var1. Otherwise, any static member (function or variable) is similarly available, so if var1 isn’t public, an “isActiveState()” public static function declared as:

public static bool isActiveState() { return var1; }

could be called from any child with Supervisor.isActiveState(), to read the bool.

With this, the children have no obstacle in checking the public static state of var1.

However, since the children so deactivated will not receive Update calls, it will be difficult to activate them from Supervisor.

I sense it may be more appropriate to perform the deactivation in the supervisor, at the moment a function to deactivate is called (and possibly to activate later). If so, there has to be a way for the supervisor to efficiently call the child or children.

The child could register for this during start. If there really is only 1 and never more than 1 child, there could be a static variable in Supervisor set to the child, so when the child’s start function is executed, it could do something like:

Supervisor.Register( GetComponent<Script>() );

Where Register is a public static function in Supervisor that sets this static Script variable, say childScript, and Script is a type, that of your child’s script. At that point, the deactivate function can call the child’s script directly with something like

childScript.gameObject.SetActive( false );

However, it is more likely there are multiple children. The same approach would work if the Supervisor had a statically held container of children, perhaps a List() (note that Script is whatever type your child script is). The supervisor could then loop through all children in this List, calling them to deactivate, or activate as required.