Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Input.GetKeyDown not working at all, even in Update()

Discussion in 'Scripting' started by Galaxy888ify, Jun 13, 2020.

  1. Galaxy888ify

    Galaxy888ify

    Joined:
    Mar 2, 2019
    Posts:
    3
    So I was writing some code and the idea is that when i press space my prefab is instantiated at my mouse pos, where an image of the currently selected prefab to place is, you can then press enter to choose the prefab, i tried using GetKey but it would fire multiple times and my enter button would not work as you would have to try multiple quick clicks for the image to switch once instead of twice or more, so I looked around and concluded GetKeyDown was my best bet, unfortunately this does not seem to work AT ALL, not even debug logging in the console, only GetKey, this is my script:

    Code (csharp):
    1.  using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClickManager : MonoBehaviour
    6. {
    7.  
    8.     public GameObject rampPrefab;
    9.     public GameObject blockPrefab;
    10.     private int selectedPrefab = 0;
    11.     private Vector3 spawnPos;
    12.     private Vector3 mousePos;
    13.     public GameObject rampImg;
    14.     public GameObject blockImg;
    15.     public Vector3 offset;
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         blockImg.SetActive(false);
    22.         rampImg.SetActive(true);
    23.         selectedPrefab = 0;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.  
    30.         mousePos = Input.mousePosition;
    31.         mousePos.z = 2.0f;
    32.         spawnPos = Camera.current.ScreenToWorldPoint(mousePos);
    33.  
    34.         rampImg.transform.position = mousePos;
    35.         blockImg.transform.position = mousePos;
    36.  
    37.         if (Input.GetKeyDown(KeyCode.Return))
    38.         {
    39.             if (selectedPrefab == 1)
    40.             {
    41.                 selectedPrefab = 0;
    42.                 blockImg.SetActive(false);
    43.                 rampImg.SetActive(true);
    44.                 Debug.Log("Changed to ramp");
    45.             }
    46.  
    47.           else if (selectedPrefab == 0)
    48.             {
    49.                 selectedPrefab = 1;
    50.                 rampImg.SetActive(false);
    51.                 blockImg.SetActive(true);
    52.                 Debug.Log("Changed to block");
    53.             }
    54.         }
    55.  
    56.    
    57.  
    58.         if (Input.GetKeyDown(KeyCode.Space))
    59.         {
    60.             if (selectedPrefab == 1)
    61.             {
    62.                 Instantiate(blockPrefab, spawnPos + offset, Quaternion.identity);
    63.                 Debug.Log("Placed block");
    64.             }
    65.  
    66.             if (selectedPrefab == 0)
    67.             {
    68.                 Instantiate(rampPrefab, spawnPos, Quaternion.identity);
    69.                 Debug.Log("Placed ramp");
    70.             }
    71.         }
    72.  
    73.     }
    74.  
    75.  
    76.    
    77.  
    78.  
    79.  
    80. }


    FIXED:
    Strangely, I was playing around with the code and I decided to change spawnPos = Camera.current.ScreenToWorldPoint(mousePos); to spawnPos = Camera.main.ScreenToWorldPoint(mousePos); Somehow, this fixed my issue, sorry for bothering you all...
     
    Last edited: Jun 13, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  3. Galaxy888ify

    Galaxy888ify

    Joined:
    Mar 2, 2019
    Posts:
    3