- Home /
Door opener
Hello i am having a game where diferent buttons are going to open different doors.
The code for the button use is:
var ButtonNr : int;
//Private Variables
var isOn : boolean = false;
function Update ()
{
if(isOn)
{
//OpenDoor(ButtonNr);
}
}
public function UseButton()
{
if(isOn)
{
isOn = false;
renderer.material.color = Color.blue;
}
else if(!isOn)
{
isOn = true;
renderer.material.color = Color.green;
}
print(isOn);
}
As you can see i have commented out OpenDoor(ButtonNr) as i dont know how to call it in a diferent script. the other script looks as this:
var DoorOpen : boolean = false;
var DoorNr : int;
function OpenDoor(nr : int)
{
if( nr == DoorNr && !DoorOpen)
{
Destroy(gameObject);
}
}
i need it to test all of the doors if there button have been switch on is there a way to individualy check all doors ?
thanks for your time and i hope you will help
Answer by aldonaletto · Feb 09, 2012 at 11:52 PM
There are several ways to do that, but I would do the following:
1- Create an empty object, call it "Doors" and reset its position and rotation;
2- Child all doors to this object;
3- Modify the button script like this:
var ButtonNr : int; static var theDoors: GameObject; // reference to "Doors" object
function Start(){ // first button executing Start assign theDoors: if (!theDoors) theDoors = GameObject.Find("Doors"); }
//Private Variables var isOn : boolean = false; function Update () { if(isOn) { // call the function OpenDoor(ButtonNr) in all children of "Doors" theDoors.BroadcastMessage("OpenDoor", ButtonNr, SendMessageOptions.DontRequireReceiver); } }
public function UseButton() { if(isOn) { isOn = false; renderer.material.color = Color.blue; }
else if(!isOn)
{
isOn = true;
renderer.material.color = Color.green;
}
print(isOn);
}