2 Triggers, 2 scripts, need help communicating the two.

okay so i have 2 triggers (game objects). when one is touched by the player it sets a variable “secret” to 1. the second trigger ask “if touching the player and if secret == 1 then set secret to 2”. sounds simple right? well i keep getting errors because i have no clue how to connect the two script from two different game objects. I have searched the far corners of the internet and still can’t find a solution that is probably vary simple and I’m just probably too blind to know. here is my code:

#pragma strict
public var secret : int = 0;

function OnTriggerEnter(col : Collider) {
	if(col.tag == "Player")
	{
	secret = 1;
	}
}

and

#pragma strict
private var otherScript : Secret1;

function Start () {
    otherScript = GetComponent (Secret1);
}

function OnTriggerEnter(col : Collider){
	if(col.tag == "Player")
	{
	
		if (otherScript.secret == 1) //the problem is here. The two scripts are not in the same game object so i need a solution to substitute this.
			{
			otherScript.secret = 2;
			}	
	}
}

I need to know the best way to connect the two.

If it isn’t on the same game object then finding the component will leave you sad. To use GetComponent without the gameobject reference will leave the GetComponent method searching on the game object that the executing script is attached to.

I’ve modified your scripts a little to have it search for the GameObject that i’ve attached Secret1 on, I did change the case of your script as a FYI. I also used the generic type of GetComponent to ensure i’m not doing additional casting from type Component to the desired type.

//OtherScript.js
#pragma strict
 private var otherScript : Secret1;
 
 function Start () {
     otherScript = GameObject.Find("Secret1Object").GetComponent.<Secret1>();
     if (otherScript) {
     	Debug.Log("Found the other script by it's name");
     	Debug.Log("OtherScript secret value: " + otherScript.secret);
     }
     	
 }
 
 function OnTriggerEnter(col : Collider){
     if(col.tag == "Player")
     {
     
         if (otherScript.secret == 1) //the problem is here. The two scripts are not in the same game object so i need a solution to substitute this.
         {
             otherScript.secret = 2;
         }    
     }

 }

//Secret1.js
#pragma strict
public var secret : int = 0;
 
function OnTriggerEnter(col : Collider) {
    if(col.tag == "Player")
    {
    	secret = 1;
    }
}

You can see from the screenshot that the execution with GameObject.Find was fruitful and found the GameObject called “Secret1Object” that has the Secret1.js script attached to it.

There are other times when you may need to get components in the GameObject heirarchy as well, there are methods for that as well such as GetComponentsInChildren and GetComponentsInParent, these are the plural versions and there are singlular versions that return the first occurance of a component of a particular type. You should read the documentation for the Component type.

Lastly, don’t ask for help and state you get errors and then not post them, that silly and generally wastes everyone’s time asking you what the error is. Luckily this is such a common question and stating “I have searched the far corners of the internet and still can’t find a solution” is very frustrating given there are thousands of posts asking this same question.