Is there a way to make a boolean update in all instances of a script? (same script on duplicated gameObjects) c#?

Right now I essentially have a light switch. I am trying to make it so multiple switches can turn this light off. The problem is when one switch turns a light on the other switch doesn’t realize the light has been turned on, so it’s bool for the light being on is still false. Thanks.

	void OnTriggerEnter(Collider other){
		if (other.gameObject.tag == "Player"  && canSwitch) {
						if (isOn) {
								redLight.SetActive(false);
								canSwitch = false;
								isOn = false;

						}
						else if (isOn == false) {
								redLight.SetActive (true);
								canSwitch = false;
								isOn = true;

						}


				}
		}

Sure you can, just reference your script.

Lets say in this case, all of your light switches are parented to a game object named “Wall,” and the script that you pasted above is called “LightSwitchScript1.”

Do something like this.

void OnTriggerEnter(Collider other){
	if (other.gameObject.tag == "Player"  && canSwitch) {
		if (isOn) {
			redLight.SetActive(false);
			//switch our local canSwitch bool;
			canSwitch = false;
			//switch our remote canSwitch bool;
			GetComponent<LightSwitchScript2>().canSwitch = false;
			//switch our local isOn bool;
			isOn = false;
			//switch our remote isOn bool;
			GetComponent<LightSwitchScript2>()isOn = false;
		}
		else if (isOn == false) {
			redLight.SetActive (true);
			canSwitch = false;
			GetComponent<LightSwitchScript2>().canSwitch = false;
			isOn = true;
			GetComponent<LightSwitchScript2>()isOn = true;
			
		}
		
		
	}
}

Alternatively, you can create something called a quick reference, which will save you from having to GetComponent<>() every single time, and will ultimately save resources.

And that would look like this:

void OnTriggerEnter(Collider other){
	if (other.gameObject.tag == "Player"  && canSwitch) {
		switch2 = GetComponent<LightSwitchScript2>();
		//tell the script what we'll be calling "LightSwitchScript2" from now on,
		//while we're in this if statement.

		//Be SURE to declare this as a private or public variable like this:
		//private variable LightSwitchScript2 switch2;
		if (isOn) {
			redLight.SetActive(false);
			//switch our local canSwitch bool;
			canSwitch = false;
			//switch our remote canSwitch bool;
			switch2.canSwitch = false;
			//switch our local isOn bool;
			isOn = false;
			//switch our remote isOn bool;
			switch2.isOn = false;
		}
		else if (isOn == false) {
			redLight.SetActive (true);
			canSwitch = false;
			switch2.canSwitch = false;
			isOn = true;
			switch2.isOn = true;
			
		}
		
		
	}
}

Be aware, however, that if your lightswitches are NOT childs of the same game object in the hierarchy, that you will need to have unity search for the GameObject first!

That looks like this:

		GameObject.Find("LightSwitch2").GetComponent<LightSwitchScript2>();

		or 

			GameObject.FindObjectsWithTag("LightSwitch").GetComponent<LightSwitchScript2>();

and hi again, well a way to do it is to use static variables ( or make a light manager). (static variables always stay the same)
soo…

public static bool isOn = true;
public static bool canSwitch = true; // and thats it , should work, mark if correct as correct answer


void OnTriggerEnter(Collider other){
        if (other.gameObject.tag == "Player"  && canSwitch) {
                        if (isOn) {
                                redLight.SetActive(false);
                                canSwitch = false;
                                isOn = false;
 
                        }
                        else if (isOn == false) {
                                redLight.SetActive (true);
                                canSwitch = false;
                                isOn = true;
 
                        }
 
 
                }
        }

One way to achieve this is to make the variable that controls the light (isOn) static.

So define your variable as:

 static bool isOn;

This will make sure that there is only one instance of isOn, shared by all the objects.

A disadvantage of this is that you can only have one instance of isOn this way.

You already have a reference to the light you are switching, so in every lightswitch you just need to “ask” the light whether it’s on or not.

In fact you can probably get rid of isOn altogether.

void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Player"  && canSwitch) 
		{
			if (redLight.activeSelf)
			{
				redLight.SetActive(false);
				canSwitch = false;
			}
			else
			{
				redLight.SetActive (true);
				canSwitch = false;
			}
		}
	}

Hi. Seems to me that you want the “Light” object/component to toggle when a switch is pressed. One way to implement this would be to have a component on the light and each switch to call a method on it’s stored light. You can then have two switches referencing the same light.

public class MyLight : MonoBehaviour
{
    private bool isOn = true;

    public void Toggle()
    {
        isOn = !isOn;
        gameObject.SetActive(isOn);
    }
}

public class MySwitch : MonoBehaviour
{
    [SerializeField]
    private MyLight myLight;

    public void FlickSwitch()
    {
        myLight.Toggle();
    }
}

In the example above, I’ve let the light object be in charge of its own state and the switch only interacts with it through the public method ‘FlickSwitch’ (your trigger code would go in MySwitch and simply call this method). In the editor a gameobject with MyLight can be dragged into the “myLight” field of a MySwitch component to set up the relationship. This is generally a good way to set up component relationships.

I hope that helps =D