- Home /
How do I make it so when I press a button it only triggers untill i press it again?
So I'm trying to make a toggle-able torch and when I hold the button (t) for too long (Less then a second) it will make the torch flash. How can I change my code so that i only press?
Also whats the difference between Update and FixedUpdate?
My Code: #pragma strict
var toggle : Light; toggle.enabled = true;
function Update () {
if(Input.GetAxis("Torch")){
if(toggle.enabled){
toggle.enabled = false;
}
else {
toggle.enabled = true;
}
}
}
Its hard to understand what you want but ill make a wild guess you want some code to be executed when you press a button and continue to be executed until you press it again? One way is to use a coroutine that gets started the first time you click and stopped the next time. http://docs.unity3d.com/ScriptReference/Coroutine.html
No i want the code to only execute once when i press a button, so say if i hold the key "t" down it will still only execute once, and it will keep the light in that same state until "t" is pressed again. I hope this is more understandable:/.
Answer by SenorGoatMan · Jun 26, 2014 at 09:30 PM
I would assume you want the GetButtonUp() function.
if(GetButtonUp("Torch")){//Assuming 'Torch' is a button
if(toggle.enabled){
toggle.enabled == false;
}
else{
toggle.enabled == true;
}
}
Hope that helps
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Transition from GameMaker to Unity 1 Answer
Parent Object Via Code C# 3 Answers
How do I pause and unpause my game on the iPhone? 1 Answer
Unity-target mainly for design? 1 Answer