- Home /
Turing GameObjects On and Off again.
Hello, I am currently creating a puzzle that when you click a button it opens certain pathways but closes others. I also need the player to be able to press the button again and switch it back so here is what i am currently working with.
var pressed : boolean = false;
var laser1 : GameObject;
var laser4 : GameObject;
function OnTriggerEnter (button : Collider) {
if(button.gameObject.tag == "Player" && pressed == false) {
laser4.active = false;
laser1.active = true;
pressed = true;
}
if(button.gameObject.tag == "Player" && pressed == true) {
laser4.active = true;
laser1.active = false;
pressed = false;
}
So, the idea is that when they enter the trigger it makes one laser active and the other one false, then sets the pressed boolean to true so that when you enter the trigger again it will recognize that the boolean is true and turn them back to how they were.
However this doesn't work, since i'm sure that the second that you enter the trigger it's doing the function for when pressed is true, then because it's changed to true does the function for pressed being true straight away...
Can anyone help?
Answer by dannyskim · Apr 26, 2012 at 10:09 PM
Well, first glance at your logic, if pressed initialized as false, let's take a look:
if(button.gameObject.tag == "Player" && pressed == false) {
laser4.active = false;
laser1.active = true;
pressed = true;
}
It goes into this code block above, you switch the 'lasers', and then set pressed back to true. So, when the code block goes to the next if statement below:
if(button.gameObject.tag == "Player" && pressed == true) {
laser4.active = true;
laser1.active = false;
pressed = false;
}
it's going to execute that as well because the previous if block already changed pressed to true.
Yeah, i figured that was the problem. So i tried only switching the pressed boolean on trigger exit but that does the same thing... What's a different way of doing its?
Try this...
if(button.gameObject.CompareTag("Player")){
laser4.active = pressed ;
laser1.active = !pressed ;
pressed = !pressed ;
}
or use an if/else
if(button.gameObject.CompareTag("Player")){
if(pressed){
laser4.active = true ;
laser1.active = false ;
pressed = false ;
}
else{
laser4.active = false ;
laser1.active = true ;
pressed = true ;
}
}
Answer by TheFrankman123 · Apr 26, 2012 at 10:37 PM
Right, Got it. The problem was as 'dannyskim' stated. So the correct way of doing it is like this:
var pressed : boolean = false;
var laser1 : GameObject;
var laser4 : GameObject;
function OnTriggerEnter (button : Collider) {
if(button.gameObject.tag == "Player" && pressed == false) {
laser4.active = false;
laser1.active = true;
pressed = true;
} else {
laser4.active = true;
laser1.active = false;
pressed = false;
}
}
Actually I take this answer back... it's rubbish way of doing it.
wtf is going on with this page? all of these other posts weren't displayed earlier (or the last few times I clicked into this topic) after/before I posted my comment earlier. ... The one by @daringly wasn't displayed just a second ago when I left, then came right back in here now and can see it.
Answer by daringly · Apr 26, 2012 at 11:26 PM
Yes, this seems like a problem, because if something else hit the collider and triggered it, the "else" would be executed. You could always make it an "else if" and do a player tag check.
Answer by TheFrankman123 · Apr 27, 2012 at 10:35 AM
I the problem with this is that if say you added a button click into it. It would change the boolean to true/false whenever you walked into the trigger area regardless of whether you pressed the button or not. Meaning that if you walked through the trigger area and didn't press the button, then next time you pressed the button it would behave the wrong way.
Another issue is that when you put a button press in to activate it for some reason it doesn't work the first time you enter the trigger... very strange behavior.
They way i have resolved this when you enter the trigger and it changes which objects are one and off it adds a red glow around the trigger area to show it's activation.
This is not really a solution since it only works in the way my game functions and the puzzle you are completing.
Would be good to see a proper solution though.