- Home /
Multiple Trigger Script doenst works!
Im having 2 pressure plates, both needs to be activated to destroy a Door. Why arent my scripts working?
DestroyDoor.js
#pragma strict
var box1 : GameObject;
var box2 : GameObject;
var door : GameObject;
var pressurePlateApressed : PressurePlateAScript;
var pressurePlateBpressed : PressurePlateBScript;
function Start () {
}
function Update () {
}
function Destroy () {
if (pressurePlateApressed && pressurePlateBpressed){
Destroy(door);
}
}
pressureplateascript (b is the same way)
#pragma strict
function Start () {
}
function Update () {
}
function OnTriggerEnter( box ) {
if (gameObject.name == "Box") {
Debug.Log("A");
pressurePlateApressed = true;
}
}
Answer by Chronos-L · Mar 30, 2013 at 10:48 AM
It will not work because:
your second script will not compile
your Destroy function is not called at all
you will destroy the door if A and B is assign to the first script, whether or not they are pressed is irrelevent
Script 1
pragma strict
var box1 : GameObject; var box2 : GameObject; var door : GameObject; var plateA: PressurePlate; var plateB: PressurePlate;
function Start () {
}
function Update () { Destroy(); }
function Destroy () { if ( plateA.pressed && plateB.pressed ){ Destroy(door); } }
PressurePlate.js
pragma strict
var pressed : boolean;
function OnTriggerEnter( other : Collider ) { if (other.gameObject.name == "Box") { pressed = true; } }
Irrelevant, but still important
You should work harder on your programming skill if you are going to keep on doing this.
You have a pressureplateascript
and a pressureplatebscript
, and they have the same behaviour, then why not use the same script for both A and B?
If I have 8 bots in a death-match and each bots have a revolver and a rifle, does that mean I have to write 24 different scripts?
In your pressureplateascript, you have a pressurePlateApressed
in the OnTriggerEnter()
, but you never define it in the script. Your script will not compile.
You have a passable logic in your Destroy()
, but you never call that function at all, of course it wouldn't work. Not using a function is almost equal to not writing one, just that the latter doesn't waste the programmer's time and effort.
It is better to be harsh to you right now, it will make you start learning on the basic; I did this so that you will not shoot yourself in the leg when you start to work on scripts that is much more complex.
Now the Door opens if i only activate one Pressure Plate??
no, you have to activate both A and B.
You do not need 2 scripts for the same behaviour. When you assign PressurePlate.js to A and B, they will create their own copy of the script. $$anonymous$$aking changes to A will not affect B.
All gameobject has a transform component, will changing the scale of gameobject affects another irrelevant gameobject? No, because one transform belongs to one gameobject, in the similar sense, one PressurePlate.js to belongs to one gameObject.
Now the Door didnt open WTF EDIT; If i activate one Pressure Plate, the Door opens. Why I want that the door opens f i activae both PPs
The current if-statement condition in my answer: plateA.pressed && plateB.pressed
means the door will be destroyed when both A and B is activate.
If you need just 1 to be activated to destroy the door, then change to plateA.pressed || plateB.pressed
.
The condition plateA.pressed && plateB.pressed
means that both of them need to be touched to Destroy(door)
. The logic have no problem.
Your answer
Follow this Question
Related Questions
Problem With Enemy AI 0 Answers
My monster only choose one Target 2 Answers
Make Deaths end game 1 Answer
Vehicle help 1 Answer
if statement error 1 Answer