Referencing a Boolean from another script/GameObject

Ok so what I want to do is create a “Activate Object” script that can be applied to a trigger, then used to tell objects if they are activated (allowing the player to use them). For instance say a player walks up to a door, the trigger in front of the door would switch to “objectActivated = true" and then the player would be able to use the door. Pretty much like a “use” button in any FPS. My issue is that I can’t figure out how to pull Information from the Activate Object script into the other script that needs to reference it. I can get both scripts to work separately but not together. Also, id like to be able to assign each trigger area to the object its activating via gameobject variable. That way each trigger area only activates what its attached to. Here are the scripts I’m working on. Keep in mind though I’ve only been programing for a day or two.
I realize that my door script inst correct.
Activate Object Script

var objectActivated : boolean = false;

function OnTriggerEnter (other : Collider) {

		objectActivated = true;

}

Door Script

var doorIsOpen : boolean = false;
var doorIsClosed : boolean = true;
var activationArea : GameObject;



function Update () {    
     if(objectActivated == true){
     	DoorControls();
    }
}
     
function DoorControls(){
	if(doorIsClosed == true){
		OpenDoor();
	}
		else{
		CloseDoor();
	}
}
	
function OpenDoor () {
	if(Input.GetKeyDown("f")){
		animation.Play("Take 001");
		doorIsOpen = true;
		doorIsClosed = false;
	}
}

function CloseDoor () {
	if(Input.GetKeyDown("f")){
		animation.Play("Take 002");
		doorIsOpen = false;
		doorIsClosed = true;
	}
}

Hi there!

To reference a variable from another script, you’ll need a GetComponent ([Script Reference)][1] This allows you to access variables from components either on the same object the script is on or other objects.

So in the ‘Door Script’, add two variables called:

var objectActivatedObj : GameObject;
private var objActScript : ActivateObject;

And in the inspector, you’d add the gameobject which the object activated script is attached to. Then, before function Update () put:

function Awake () {
objActScript = objectActivatedObj.GetComponent(ActivateObject);
}

That should provide you with the reference to the Activate Object script. And finally, in if(objectActivated == true){, make it:

if(objActScript.objectActivated == true){

And then that should work. :slight_smile:

Klep
[1]: http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

Referencing data in other scripts is SUPER IMPORTANT. You should read these two pieces from the Scripting Reference for all the ways to access scripts in other objects (once you do, you probably won’t need my suggestions, but feel free to read them!):

http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Components.html
http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

Scripts can hold references to other scripts (which are attached to other objects in your scene), which allows them to modify the referenced script’s behavior.

Here’s what you can do: Change your Activate Object Script to this:

public var door : DoorScript;

function OnTriggerEnter (other : Collider) {

       door.Activate();

}

Now in your editor’s inspector, the object you have your Activate Object Script attached to (your trigger) will have a field that says “Door” and a little box that says "None (DoorScript). Click on door you want to attach to this trigger and drag it into the box that says None. Now it will have the name of your Door.

Now to modify your DoorScript:

var doorIsOpen : boolean = false;

public function Activate() {
    if (doorIsOpen) {
       CloseDoor()
    }
    else {
       OpenDoor()
    }
}

function OpenDoor () {
    if(Input.GetKeyDown("f")){
       animation.Play("Take 001");
       doorIsOpen = true;
    }
}

function CloseDoor () {
    if(Input.GetKeyDown("f")){
       animation.Play("Take 002");
       doorIsOpen = false;
    }
}

Notice how we reduced the number of boolean variables we have to keep track of (two, instead of four), and we got your open/closing door calls out of the Update function (which would have just caused them to open and close constantly). You can now attach your Activate Object script to any trigger, and as long as you assign a Door to it, when it’s triggered it will open that door.

There are a ton of other things you could do with your Activate Object script, like make it so that it works with any kind of object that has an Activate() function or specify the trigger to only work when players or NPCs trigger it, but you’ve only been programming for two days. Keep at it!