Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Input.GetKeyDown Help

Discussion in 'Editor & General Support' started by Johnnoo, Jul 30, 2020.

  1. Johnnoo

    Johnnoo

    Joined:
    Aug 1, 2018
    Posts:
    4
    I'm a beginner with C# and Unity and can't figure out why this script doesn't work.

    Code (CSharp):
    1. public class boom : MonoBehaviour
    2. {
    3.     public Object prefab;
    4.  
    5.     // Update is called once per frame
    6.     void Update()
    7.     {
    8.         if (Input.GetKeyDown("a"))
    9.         {
    10.             Debug.Log("Button Pressed");
    11.             Breakblock();
    12.         }
    13.     }
    14.  
    15.     public void Breakblock()
    16.     {
    17.         Debug.Log("Function called");
    18.         Instantiate(prefab, transform.position, transform.rotation);
    19.         Destroy(gameObject);
    20.     }
    21. }
    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.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Check if Update is even running by adding another debug.log outside of the if statement inside Update.
     
  3. Hi_ImTemmy

    Hi_ImTemmy

    Joined:
    Jul 8, 2015
    Posts:
    174
    I'd echo this. What is this script attached to? Make sure what ever gameobject it's on is enabled in the scene.
     
  4. Johnnoo

    Johnnoo

    Joined:
    Aug 1, 2018
    Posts:
    4
    Yep, Update is running
     
  5. Johnnoo

    Johnnoo

    Joined:
    Aug 1, 2018
    Posts:
    4
    It's attached to a prefab of a standard cube. It's enabled in the scene
     
  6. Hi_ImTemmy

    Hi_ImTemmy

    Joined:
    Jul 8, 2015
    Posts:
    174
    @Johnnoo I've just tried it in a blank project and the output hits the console just fine.
    upload_2020-7-30_22-57-24.png

    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.
     
  7. Johnnoo

    Johnnoo

    Joined:
    Aug 1, 2018
    Posts:
    4
    Annotation 2020-07-30 190123.png
    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
    Annotation 2020-07-30 191432.png
    Annotation 2020-07-30 191630.png
    I made a new project too just to check and it doesn't work there either. Annotation 2020-07-30 193330.png
     
    Last edited: Jul 31, 2020
  8. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Are you pressing A with the Game tab open (as opposed to the scene tab)?
     
    PraetorBlue likes this.
  9. Hi_ImTemmy

    Hi_ImTemmy

    Joined:
    Jul 8, 2015
    Posts:
    174
    Couple of things to check;

    Does the very top of your script look like this?

    upload_2020-7-31_8-59-50.png



    When pressing play, does the 'Game' tab become active?

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

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