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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input not working for specific script!

Discussion in 'Scripting' started by WasifBoomz, Jan 24, 2019.

  1. WasifBoomz

    WasifBoomz

    Joined:
    Apr 12, 2017
    Posts:
    3
    So, I'm making a level editor for my game, and the input is tragically not working!
    I searched everywhere and I couldn't find the answer!
    If I go to a pre-built level the input works perfectly!
    I tried putting the script on a different object, that didn't work either...
    Whoever solves this will get credited in the credits,
    BTW, don't judge my script it's not even close to done.
    THANKS!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class Level_Editor : MonoBehaviour
    6. {
    7.     public GameObject grid;
    8.     [Header("Background Color")]
    9.     public InputField RF;
    10.     public InputField GF;
    11.     public InputField BF;
    12.     public Text fText;
    13.     Color backGroundColor = new Color(0, 0, 0);
    14.     [Header("Ground Color")]
    15.     public InputField RG;
    16.     public InputField GG;
    17.     public InputField BG;
    18.     public Text gText;
    19.     Color foreGroundColor = new Color(0, 0, 0);
    20.     private Camera cam;
    21.     public GameObject obj;
    22.     Vector3 hitPosition;
    23.     // Start is called before the first frame update
    24.     private void Start()
    25.     {
    26.         cam = FindObjectOfType<Camera>();
    27.  
    28.  
    29.     }
    30.  
    31.  
    32.     // Update is called once per frame
    33.     public void Update()
    34.     {
    35.         grid.transform.position = new Vector2(Mathf.Round(Camera.main.transform.position.x), Mathf.Round(cam.transform.position.y));
    36.         backGroundColor = new Color(int.Parse(RF.text), int.Parse(GF.text), int.Parse(BF.text));
    37.         cam.backgroundColor = backGroundColor;
    38.         foreGroundColor = new Color(int.Parse(RG.text), int.Parse(GG.text), int.Parse(BG.text));
    39.         gText.color = foreGroundColor;
    40.         if (Input.anyKey)
    41.         {
    42.             Debug.Log("?");
    43.             Vector3 pz = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    44.             pz.z = 0;
    45.             gameObject.transform.position = pz;
    46.             Instantiate(obj, pz, transform.rotation);
    47.         }
    48.     }
    49.     public void DisableObj(GameObject gg)
    50.     {
    51.         gg.active = false;
    52.     }
    53.     public void EnableObj(GameObject gg)
    54.     {
    55.         gg.active = true;
    56.     }
    57.  
    58. }
     
    Last edited: Jan 25, 2019
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    First you need to be specific what exactly "not working" means.
     
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    You mean 'Input.anyKey' returns false even when a key is presses?

    Like @Joe-Censored said, you gotta give us some more detail,
    how did you try to debug it?
    are you sure the Update loop is even running?
    where is this all taking place?
     
    Joe-Censored likes this.
  4. thomasotje10

    thomasotje10

    Joined:
    Apr 6, 2015
    Posts:
    19
    It works fine for me. Have you tried
    Input.anykeyDown
    instead?
    Or maybe your script is not enabled in the inspector?
    Have you put a Debug.log() outside your if-statement to see if it runs?

    You can declare your variables like this btw:
    public InputField RF, GF, BF;

    It will have the same effect, but looks alot cleaner.
     
  5. WasifBoomz

    WasifBoomz

    Joined:
    Apr 12, 2017
    Posts:
    3
    @Joe-Censored: I tried all kinds of input and it did not respond(I also tried this with a UI button)
    @SparrowsNest: First, I searched up my threads of a similar problem, did not help.
    Yes, it is running because my grid snaps when I move the camera through the inspector.
    A level editor, there is a canvas with other canvases inside it. There is a grid too. The script is on my main canvas.
    @thomasotje10: I tried that and other kinds of input too including Horizontal, Fire 1 and MouseButtonDown.
    It's enabled, my IQ is over 3.
    When I turned the click into a UI button, the Debug.Log worked.
    Also, thanks for the tip, even though it messes up the headers!
    A credit to you!
    THANKS ALL OF YOU!
    CaptureHF.PNG