Controlling two toggles applied in two gameobject scripts through a third script

Hello everyone!

I am a new user, and this is my very first post here.

Problem description:
I have two toggles (toggle1, toggle2) defined as public variables in two different scripts (toogle1Script, toggle2Script). These two scripts are applied on two empty gameobjects (emptyGameObject1, emptyGameObject2). Also, I have a third empty gameobject (emptyGameObject3) on which I have an another script (toggleControlScript), through which I want to control (ON/OFF) the two toggles.

I want toogle2 to respectively become ON/OFF (true/false) when toggle1 is clicked ON/OFF (true/false), and vice versa. In simple word, two toggles should copy and correspond each other actions.

The three Java Scripts are as follows:

First script-

//toogle1Script
//emptyGameObject1 > toogle1Script > toggle1

var toogle1:boolean=false;

function OnGUI () 
{
 toggle1=GUI.Toggle (Rect (200, 180, 80, 80),toggle1, "Toggle1");
}

Second script-

//toogle2Script
//emptyGameObject2 > toogle2Script > toggle2

var toogle2:boolean=false;

function OnGUI () 
{
 toggle2=GUI.Toggle (Rect (400, 180, 80, 80),toggle2, "Toggle2");
}

Third script-

//toggleControlScript
//emptyGameObject3 > toggleControlScript

private var toggle1Control:boolean=false;
private var toggle2Control:boolean=false;

function Update()
{
 toggle1Control=gameObject.Find("emptyGameObject1").GetComponent("toogle1Script").toggle1;
 toggle2Control=gameObject.Find("emptyGameObject2").GetComponent("toogle2Script").toggle2;
 
 if (toggle1Control)
 {
 toggle2Control=true;
 }
 else
 {
 toggle2Control=false;
 }
 
 if (toggle2Control)
 {
 toggle1Control=true;
 }
 else
 {
 toggle1Control=false;
 }
}

I appreciate your replies before long, as I have a university submission soon.

Thanks!
Vaibhav Jain

For this to work, you should detect when one of the toggle variables changed state, and copy it to the other toggle variable. To avoid wasting time searching for the objects and the scripts each Update, do it only once at Start (all Find functions are too slow to be used at Update):

private var tog1Script: toogle1Script; // reference to toogle1Script
private var tog2Script: toogle2Script; // reference to toogle2Script
private var lastTog = false; // keeps the last state

function Start(){
  // get the script references at Start to improve performance:
  tog1Script = gameObject.Find("emptyGameObject1").GetComponent(toogle1Script);
  tog2Script = gameObject.Find("emptyGameObject2").GetComponent(toogle2Script);
}

function Update(){
  if (tog1Script.toggle1 != lastTog){ // if toggle1 changed...
    tog2Script.toggle2 = tog1Script.toggle1; // copy to toggle2...
    lastTog = tog1Script.toggle1; // and update lastTog
  }
  else
  if (tog2Script.toggle2 != lastTog){ // if toggle2 changed...
    tog1Script.toggle1 = tog2Script.toggle2; // copy it to toggle1...
    lastTog = tog2Script.toggle2; // and update lastTog
  }
}