Input.GetKeyDown Help

I’m a beginner with C# and Unity and can’t figure out why this script doesn’t work.

public class boom : MonoBehaviour
{
    public Object prefab;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("a"))
        {
            Debug.Log("Button Pressed");
            Breakblock();
        }
    }

    public void Breakblock()
    {
        Debug.Log("Function called");
        Instantiate(prefab, transform.position, transform.rotation);
        Destroy(gameObject);
    }
}

When I press A nothing happens; nothing in the console, nothing in the hierarchy changes.
It isn’t paused and I have dragged the prefab into the inspector.

Check if Update is even running by adding another debug.log outside of the if statement inside Update.

I’d echo this. What is this script attached to? Make sure what ever gameobject it’s on is enabled in the scene.

Yep, Update is running

It’s attached to a prefab of a standard cube. It’s enabled in the scene

@Johnnoo I’ve just tried it in a blank project and the output hits the console just fine.

The script is OK, it must be something in your scene setup. Please take a few screenshots of your scene setup and the gameobject this script is used on. We’ll have to see more to get an idea of what is going on.


The script is on a cube that I imported from blender (blue) and I’m trying to instantiate the same cube but cell fractured (red).
I am using Unity 2019.2.17f1


I made a new project too just to check and it doesn’t work there either.

Are you pressing A with the Game tab open (as opposed to the scene tab)?

1 Like

Couple of things to check;

Does the very top of your script look like this?

When pressing play, does the ‘Game’ tab become active?

6153804--672453--upload_2020-7-31_9-0-22.png

Try pressing play and click inside the game window (it might be a focus issue).