Same button bug

Hi I want to ask a question why when I use if (Input.GetKey(KeyCode)) and use it 2 times in the update function it stops working I can’t figure out how to fix it

Input.GetKey(KeyCode) will definitely work no matter how many times you call it. Can you share your code? Otherwise we can’t do much to help.

1 Like

This is usually the least helpful phrase you can include in a question as it could mean anything. Like arkano22 said, without more context, specifically the code that “doesn’t work”, how are we supposed to help you? Should be suggest some random actions? Maybe take a nap, drink 3 sips of water, run twice around your house and return to your project. Look at your code again and it may help to suddenly realise what you did wrong. The computer never does things wrong. If it does it is broken and needs to be replaced. In 99.9% of all cases the issue is in your code. That may be due to a mistake / typo or a misunderstanding.

Yes no problem

using System. Collections;
using System.Collections.Generic;
using UnityEngine;

public class flashipickupd : MonoBehaviour
{
public static bool on;
public GameObject flashlihgt;
public static bool on_fl;

void Start()
{
on_fl = true;
on = false;

}

void Update()
{
if (on == true)
{
if (Input.GetKey(KeyCode.Mouse1))
{
flashlihgt.SetActive(false);
on_fl = false;
}

}

if (on_fl == false)
{
if (Input.GetKey(KeyCode.Mouse0))
{
flashlihgt.SetActive(true);
on = true;
}
}

}
}
and when I type if (Input.GetKey(KeyCode.Mouse1)) two times the code stop working?

I still don’t quite get your “stop working” here. Given the code both of your Input.GetKey lines would even execute because you set “on” to false and “on_fl” to true in Start. So none of the two if statements would be reached unless some external influence would change one or both of those variables. Adding another Input.GetKey if statement at the end would certainly not influence what those two statements currently do. What exactly are you doing in that “additional” GetKey if body? Do you actually mess with the boolean variables here? If so, how? What are you doing?

Look, you essentially said that something does work before you make a certain change and that it doesn’t work anymore after the change. The code you posted is pretty pointless because:

  • None of the relevant code does currently execute given the code and setup information you have provided.
  • It’s not clear what this code is supposed to do and why there are two boolean variables in the first place. It looks like a flash light toggle, but we don’t really know as none of the code would actually run given the initial values.
  • What exactly did you add in that third if statement that you added to your code? What does it do? Does removing that third statement make the old code working again?

We can not read minds. Also, please use code tags when posting code. There are two buttons to post code. Use the insert code button. It opens a window where you should copy and paste your code. It will ensure spaces are kept and it’s properly formatted as code. The “inline code” button should only be used for single fragments of code included in a text line. For anything else, use the normal insert code button.

Some code here