Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Input.GetMouseButtonDown never returns true

Discussion in 'Scripting' started by ThatOneCake, Oct 27, 2023.

  1. ThatOneCake

    ThatOneCake

    Joined:
    Dec 2, 2018
    Posts:
    36
    So this is really wierd but whenever i try to put down a beutifull Input.GetMouseButtonDown it never works...
    ive never encountered something like this before and its kinda a pain but i cant seem to fix it. and if i print the output it always says false.

    my code looks something like this
    Code (CSharp):
    1. private void Update()
    2.     {
    3.        
    4.         if (cursor != null && Camera.current != null)
    5.         {
    6.             var x = Camera.current.ScreenToWorldPoint(Input.mousePosition);
    7.             Vector2Int pos = new Vector2Int(Mathf.RoundToInt(x.x), Mathf.RoundToInt(x.y));
    8.             cursor.position = new Vector3(pos.x, pos.y, transform.position.z);
    9.  
    10.             if (Input.GetMouseButtonDown(0))
    11.             {
    12.                 print("omg");
    13.             }
    14.  
    15.         }
    16.     }
    Thanks for any help
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    758
    Use Camera.main instead of Camera.current. And make sure your camera is actually tagged MainCamera
     
    Bunny83 likes this.
  3. ThatOneCake

    ThatOneCake

    Joined:
    Dec 2, 2018
    Posts:
    36
    Thank you so much for the help... mind explaining why that worked for future reference because i really thought it wouldnt but it did.
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    758
    It's very unusual to use Camera.current and so I'm surprised you've not encountered the problem earlier.

    When to use Camera.current can be found here.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,845
    Camera.current is only set during rendering and could change depending on the context. You should not use it in Update which is pre-rendering. Also Camera.current could even reference the internal camera of the scene view inside the editor, again all depending on the context. In multi-camera rendering setups (like VR) Camera.current may reference either the left or right eye, again depending on the context.

    So as @zulo3d said, in most cases you would use Camera.main or setup a public Camera variable and assign the camera you want to use.