- Home /
I need help with my Door Switch Configuration.
I have it setup so that I have 3 sets of 2 switches for each door (3) in my project. Everything was going well until I tested the switches out. All the doors in my program open simultaneously and that is not what I want. I want them to open individually. Below I have two javascript files for this. ObjectSetupSystem (On the Player Controller) is to setup the system of switches and Object Switch Object is for the switches themselves. My goal, again, is to have all the doors open individually other than all together.
1st FILE - ObjectSwitchSystem
enter code here//###########################PressureSwitch 1#######################
var switch1Name = "Pressure Pad1";
var switch1Bool : boolean = false;
var switch1 : GameObject;
//###########################DoorSwitch 1#######################
var switch2Name = "Wall Switch1";
var switch2Bool : boolean = false;
var switch2 : GameObject;
//###########################PressureSwitch 2#######################
var switch3Name = "Pressure Pad2";
var switch3Bool : boolean = false;
var switch3 : GameObject;
//###########################DoorSwitch 2#######################
var switch4Name = "Wall Switch2";
var switch4Bool : boolean = false;
var switch4 : GameObject;
//###########################PressureSwitch 4#######################
var switch5Name = "Pressure Pad3";
var switch5Bool : boolean = false;
var switch5 : GameObject;
//###########################DoorSwitch 3#######################
var switch6Name = "Wall Switch3";
var switch6Bool : boolean = false;
var switch6 : GameObject;
//############################DOOR SWITCH 1/2###################
var door1Bool : boolean = false;
var door1 : GameObject;
//############################DOOR SWITCH 3/4 ###################
var door2Bool : boolean = false;
var door2 : GameObject;
//############################DOOR SWITCH 5/6 ###################
var door3Bool : boolean = false;
var door3 : GameObject;
function Update () {
if(switch1Bool == true) {
if(switch2Bool == true) {
door1Bool = true;
door1.gameObject.transform.Translate(Vector3.up * Time.deltaTime);
}
}
if(switch3Bool == true) {
if(switch4Bool == true) {
door2Bool = true;
door2.gameObject.transform.Translate(Vector3.up * Time.deltaTime);
}
}
if(switch5Bool == true) {
if(switch6Bool == true) {
door3Bool = true;
door3.gameObject.transform.Translate(Vector3.up * Time.deltaTime);
}
}
}
2nd File - Object Switch (For the buttons themselves)
var Player : GameObject;
var bullet : GameObject;
function OnTriggerEnter(other : Collider) {
if(Player.tag == "Player") {
Player.GetComponent(ObjectSwitchSystem).switch1Bool = true;
Player.GetComponent(ObjectSwitchSystem).switch3Bool = true;
Player.GetComponent(ObjectSwitchSystem).switch5Bool = true;
Debug.Log("Activated PressureSwitch");
}
if(bullet.collider.tag == "Bullet") {
Player.GetComponent(ObjectSwitchSystem).switch2Bool = true;
Player.GetComponent(ObjectSwitchSystem).switch4Bool = true;
Player.GetComponent(ObjectSwitchSystem).switch6Bool = true;
Debug.Log("Activated Wall Switch");
}
}
Please explain to me as how I can write this code better. I know that Im calling for all the switches to be activated the same time, I just wanna know how I could write it so they could be opened individually.
This is a bit messy as im assure you're aware of. If i understand correctly, try to design the code to be places on individual doors etc. So you dont have to tell everything what it belongs to. And whenever you find yourself copy pasting code, there is most likely a better way. No point giving you the answer here, you need to learn more of the basics, watch tutorials and read. After a while these things will be a piece of cake.
Answer by hoy_smallfry · May 13, 2013 at 07:30 PM
Your problem is that you're setting all the switches to true in OnTriggerEnter. So when you hit one switch, all the switches become true.
I think your fundamental understanding of script instancing is incorrect, just from observing the way you've set up your code. You're trying to lump every object you're creating into the scripts, and then you're assigning them for each switch and door? That makes it difficult to add to your game. Like, take these scanarios for instance:
What if you make more doors in the level? You'd have to add in more variables in the first script.
What if you want to make different kinds of switches for a door? you'd have to do some huge if-statement shenanigans.
What if when you want to use this script for another level, but that level has a different number of doors?
My point is, you gotta design more generally. Break your scripts up into simpler, smaller scripts. That way, each one is specially designed to do their own separate job. They only feed the others information they need. Just like parts in a car or a clock, your switch system is machinery too, so it makes no sense to have your cogs fused together in one big piece.
Now, you want a cog to be blueprinted in a way that you can make many cogs easily, without having to program as much and in a way that they can all work together easily. This relates to instancing scripts. Instancing happens whenever you assign a script to a game object; That game object gets it's own set of the same variables.
That means if you define 3 variables (A, B, C) in a script, and assign that script to 2 different game objects (X, Y), there are 6 variables total. You have X's instances of A, B, C and Y's instances of A, B, C. X's A is independent of Y's A, but A, B, C for each are meant to work as one cog. So, by creating instances of the objects, you can create many cogs that are independent of each other, but can work together.
So, why not have a a "bool" for each ObjectSwitch instead of all those variables in the ObjectSwitchSystem? That way, each ObjectSwitch is the one in charge of knowing what state it's in. When that instance of the switch detects a OnTriggerEnter, it's job is to of notify the ObjectSwitchSystem in some way. the ObjectSwitchSystem doesn't need to be aware of how many switches are connected to it, because the switches control the system, not the other way around.
Also, you could probably separate each door into its own switch system, that way you can coordinate which switches belong to which system. If the switches know the system they are connected to, then they can notify it whenever anything noteworthy happens, such as a trigger collision. When that notification is sent, the system can then do whatever it's been programmed to do on activation, such as opening a door.
SwitchSystem script example - Attach this to each door or anything you want to be controlled by a switch:
// For now, it will just open when any switch connected to it tells it to.
// But, you can check whatever conditions you want to see if the door is
//ready to open. Like, maybe you want to make it so that the switch only
// opens if all switches connected to it are pressed. You should probably
// extend this script instead of rewriting it if you want to add all this
// various functionality to it. Same thing with the Switch script.
function Activate()
{
transform.Translate(Vector3.up * Time.deltaTime);
}
Switch script example - Attach this to each switch you want to control a system:
// These variables can be assigned in the inspector. Attach the SwitchSystem
// script to a door object, and attach this script to your switch object.
// Then, select the switch you want to connect to the door so that you can
// see these variables in the inspector. Finally, and drag the door to where
// the 'switchSystem ' variable is, which will link that system
// script in this switch script, creating a switch / system connection.
var switchSystem : SwitchSystem; // so it can know which door to activate.
var colliderTag : String = "Player"; // so it knows which tag it should respond to.
function OnTriggerEnter(other : Collider)
{
if(Player.tag == colliderTag)
{
switchSystem.Activate();
}
}
When you design with these concepts in mind, the scripts become that simple.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Marching Cubes for Unity in JavaScript [Concept] 1 Answer
Problem with Javascript updating variables. 1 Answer
Climb animation is glitchy and isnt working properly 0 Answers
Sprint Error script? 2 Answers