- Home /
Object activation...
Hello. This is my code for a star fox style shield (the button is pressed, the shield object is active for a second, and then it is not active). If I press q that happens, the shield become active and then after a second, it deactivates. The problem is that I cannot do it again, when I press q nothing happens. What is wrong? Thanks
function Update(){ if( Input.GetKeyDown( "q")){Invoke("ShieldON",0);} else {Invoke("ShieldOFF",1);} }
function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }
What behaviour do you want? when you press and hold the button down the shield should be on and when you release the button after 1 sec it goes off? or just when you press "q" it turns on for a sec?
When you press q it turns on for a second (if that works, I could probably figure out some sort of cooldown system).
Answer by Bunny83 · Jan 11, 2011 at 03:49 AM
Your problem is that you invoke ShieldOFF every frame (every invoke get delayed 1 sec but after that sec ShieldOFF is called ... every frame)
Input.GetKeyDown() is only true for one frame (because it just tells you the event "Keydown") that means in the next frame ShieldOFF is called again.
The second script that Justin Warner posted above should work.
You can change your script to:
function Update(){ if( Input.GetKeyDown( "q")){ ShieldON(); Invoke("ShieldOFF",1); } }
function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }
ShieldON don't need to be invoked that way when you don't want a delay. Just call the function. Now when you press "q" ShieldON is called instantly and turn your shield on. But also Invoke ShieldOFF with 1 sec. delay. After that sec. ShieldOFF should be called.
Answer by Justin Warner · Jan 11, 2011 at 12:57 AM
function Update(){ if(Input.GetKeyDown("q")) { Invoke("ShieldON",0); Debug.Log("Shield on"); } else { Invoke("ShieldOFF",1); } }
function ShieldON (){ SHIELD.active=true; } function ShieldOFF(){ SHIELD.active=false; }
Try that, and tell me when/if it keeps saying Shield On in the log...
Also, this might work:
function Update(){ if(Input.GetKeyDown("q")) { Invoke("ShieldON",0); Debug.Log("Shield on"); } }
function ShieldON (){ SHIELD.active=true; yield WaitForSeconds(2); SHIELD.active=false; }
Reply how it goes please.
var SHIELD : GameObject; var activate = 0; function Update(){ if(Input.GetKeyDown("q") && activate == 0) { SHIELD.active=true; activate = 1; } if(Input.GetKeyDown("q") && activate == 1) { SHIELD.active=false; activate = 0; }
}
You might be able to figure it out from there. Sorry I couln't help further. I had this problem as well, and I got a solve, and I believe it was like this, but it still doesn't work =/. Good luck none-the-less!
"Shield On" appears in the log with both scripts...but it will only be added to the log once. No matter how many times I press q. The SHIELD object will also not turn on.
Does it say Shield On whenever you push q a lot though? Like, is it registering that it is being pushed multiple times?
And how are you getting SHIELD?
Yeah, it says Shield On for each button press. SHIELD (the object I want to turn on/off) is just a variable "var SHIELD : GameObject;"
I've been testing it, I know the problem, but I can't get it to work right on my own... $$anonymous$$aybe you can figure it out... The problem is, it is running every frame, so when you push down q, it enables it for a frame, which as you probably know, can be a hundreth of a second, easily... So, rather than that, you need to find a way to get it to just stop... So, the else statement can't be there... I tried a mode kinda thing, I'll edit it with that, but it didn't work... But again, maybe you'll be able to figure it out.
Your second script should do what he needs the third script is just a toggle script: press the button once = on press it again = Off
that would be easier that way: if(Input.Get$$anonymous$$eyDown("q")) { SHIELD.active = !SHIELD.active; }
Your answer
![](https://koobas.hobune.stream/wayback/20220613094853im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
activating object only on true 1 Answer
A bunch of errors that I can't fix 1 Answer
How to use if statement properly 1 Answer
My if statement won't work 1 Answer