Home > Net >  Unity not detecting F1 and F2 key presses
Unity not detecting F1 and F2 key presses

Time:05-31

I am returning to unity after 6 months. I started my fps game yesterday. Today I was trying to make C4 bomb, which worked as follows: the c4 will already be placed, you just have to activate using a remote. In the start the remote will not be in your hand, you have to press F1 to set it active. To put the remote away again, we have to press F2. But the problem is that it won't detect my F1 key press. Here's the code:

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

public class C4Script : MonoBehaviour
{
    public GameObject remote;
    private bool remoteHand;
    // Start is called before the first frame update
    void Start()
    {
        remote.SetActive(false);
        remoteHand = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F1) && !remoteHand)
    {
        remote.SetActive(true);
        remoteHand = true;
    }
    if (Input.GetKeyUp(KeyCode.F2) && remoteHand)
    {
        remote.SetActive(false);
        remoteHand = false;
    }
  }
} 

The code may be wrongly formatted, I am sorry for that. Also sorry if I didn't explain it properly. Thanks!

CodePudding user response:

The code works on my machine, do you maybe have the C4Script or the Gameobject on which it is attached disabled?

Looks like the script is never called rather then that it's not detecting the input. You could add an Debug log or debug breakpoint to the start method to check if it's ever called.

CodePudding user response:

Soo, the problem was that i am dumb. i Put this script on the remote, and at the start it disables the remote thats why it wasn't detecting my key press. I put the script on my player object and now it works fine.

  • Related