- Home /
Brake Light Button
Hey fellas i got a question in unity about brake lights i am very new to unity i developed a car game but i can't seem to get the brake lights to work i created a script but when i place script it works but when i hold (s) it keeps turning the light on and off
and i want if i press (s) it keeps the light on but when i remove my finger it stops lighting.
Here's the script i made:
function Update() {
if (Input.GetButton("s")) {
if (light.enabled == false)
light.enabled = true;
else
light.enabled = false;
}
}
can you send me the finished script thanks in advance
Answer by gregzo · Jul 26, 2013 at 08:52 AM
Hi,
No I cannot give you the finished script.
Input.GetButtonDown and Input.GetButtonUp should work in your case : lights on when s down, off when up.
Now when i press (s) it lights but i have to press it again to turn off here's the script I've done with what you told me can you troubleshot the script:
function Update() {
if (Input.GetButtonDown("s")) {
if (light.enabled == false)
light.enabled = true;
else if (Input.GetButtonUp)
{
light.enabled = false;
}
}
}
Look at the logic. Pseudocode :
if button is down
{
turn light on
}
if button is up
{
turn light off
}
Also, use the Unity Scripting Reference :
http://docs.unity3d.com/Documentation/ScriptReference/Input.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonUp.html
Upvoted answer.
i honestly don't get it when i press "s" it turns the light on when i press it again turns off that's not what i want for the brake lights i want the lights to be on if i keep holding "s" but when i remove my finger it stops the light
if (Input.GetButtonDown("s"))
{
light.enabled = true;
}
else if ( Input.GetButtonUp("s"))
{
light.enabled = false;
}
Please do a few scripting tutorials, UA won't write all your scripts for you.
Your answer
Follow this Question
Related Questions
How to make flary light reflection 1 Answer
Car wont drive again after red light 0 Answers
Find GameObject Position(x,z) and rotation(y) 1 Answer
How to turn on a light, then off with the same button. 1 Answer
Strange Lighting Glitch 1 Answer