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

C# - TextArea Interfering with Input.GetMouseButtonUp (Unity Bug ?)

Discussion in 'Scripting' started by bigboybifdick, Jan 5, 2016.

  1. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey there,

    Lately i've been surpassing myself in finding/creating S***ty bugs !!
    Here's my last one...

    When i debug.log : Input.GetMouseButtonUp(0), the event isnt detected the first time it happens.

    Here, the first and third debug.log work and the second (input.getmousebuttonup) dont.

    My SelectedState script (called in Update from a monobehaviour script)
    Code (CSharp):
    1.     public void UpdateState(){
    2.         Debug.Log ("SelectedState/Updating1");
    3.         if(Input.GetMouseButtonUp(0)){
    4.             Debug.Log("WHY ARENT YOU DEBUGING UP ???");
    5.             lastPos = Input.mousePosition;
    6.             ToPatrolState();
    7.         }
    8.  
    9.         Debug.Log ("SelectedState/Updating2");
    10.             delta = Input.mousePosition - lastPos;
    11.             delta.x = delta.x / Screen.width;
    12.             delta.y = delta.y / Screen.height;
    13.  
    14.             Vector3 targetPos;
    15.             targetPos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
    16.             targetPos = Camera.main.ScreenToWorldPoint(targetPos);
    17.             sphere.transform.position = Vector3.Lerp(sphere.transform.position,targetPos, Time.deltaTime*0.6F);
    18.  
    19.     }
    Please don't troll me, YES i've released the left button :)

    The weird thing is, it won't detect the buttonup, BUT, if i click again it works.

    EDIT : Ok, si i just found in the documentation that : it will not return true until the user has pressed the mouse button and released it again. Sorry...

    EDIT 2 : I decided to move the trigger MouseButtonUp in the update void of the script attached to the related gameobject and it still doesnt work !!

    Code (CSharp):
    1.     void Update()
    2.     {
    3.  
    4.         if(Input.GetMouseButtonUp(0)){
    5.             Debug.Log("UP");
    6.         }
    7.         currentState.UpdateState ();
    8.  
    9.  
    10.     }
    It's driving me crazy :p

    This second script is derived from monobehaviour... Unless i'm missing something, update should be called every frame... Why isnt it debuging "UP" ?

    Thanks,
     
    Last edited: Jan 7, 2016
  2. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Works fine for me just typing:

    Code (CSharp):
    1. void Update()
    2. {
    3.     if(Input.GetMouseButtonDown(0))
    4.     {
    5.           Debug.Log("Down");
    6.     }
    7.    if(Input.GetMouseButtonUp(0))
    8.     {
    9.           Debug.Log("Up");
    10.     }
    11. }
    However I am not using C# 6.0 so :/
     
  3. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Of course it should work :)
    Thats why its driving me crazy !!

    I'm sure I've already used it that way...

    I've also tried to debug it from another empty monohobehavour derived script attached to the camera, still doesnt work...

    Some precisions : it never works the first time, but it works from the second time i press the button and release it... oO

    + it works just fine with an empty project + empty scene... oO oO oO oO
    I'm sure i didnt go into the input options... What should i check ?
     
    Last edited: Jan 7, 2016
  4. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Heeeey,

    I've found the reason it wasnt working, but it doesnt make any sense to me...

    TextArea was GUIlty (huhuhu) :

    Code (CSharp):
    1.     void OnGUI(){
    2.         GUI.skin = myDataSkin;
    3.  
    4.         GUI.TextArea (new Rect(0,0,Screen.width, Screen.height), "Temps de jeu:" + myData.Game_Duration.ToString("F2") + "secondes");
    5.  
    6.     }
    I found out commenting the code, then rescaling the textarea (width/2 and height/2) and clicking outside of the area (in the corners).

    Could someone explain me why is the GUI.textarea preventing to get the input.mousebuttonup(0) ? + why does it only happen the first time you click??
     
    Last edited: Jan 7, 2016
  5. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    I have no idea, but the click box, lets call it, may have been very small and you scaled it up
     
  6. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Yep you're right, those textareas didn't need to be that large. But textareas aren't "buttons", or clickbox as you call it, so they shouldn't interfere with the Input.GetMouseButton calls.

    + there is absolutely nothing about it online, and i know Unity has updated its GUI system recently. Isn't it a Unity bug that should be reported?
     
    Last edited: Jan 7, 2016
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    In the inspector, untick 'Raycast Target' for the TextField, this will stop it blocking UI components below it.
     
  8. bigboybifdick

    bigboybifdick

    Joined:
    Feb 8, 2014
    Posts:
    60
    Hey,

    Thanks for answering.
    There is no TextField or TextArea component in my editor as i'm generating them from script.

    Thanks,
     
  9. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    Code (CSharp):
    1.  
    2.         var t = UnityEngine.UI.Text;
    3.         t.raycastTarget = false;
    Edit: Sorry just realised you are using the old OnGUI stuff, not the new 4.6 GUI elements, ignore what I said.

    However the old GUI stuff doesn't block clicks on stuff behind it, which was in fact one of the most annoying things about using it.
     
  10. TiUh

    TiUh

    Joined:
    Nov 16, 2015
    Posts:
    4
    I tripped on the same Problem. When clicking on an Input.TextField calling Input.GetMouseButtonUp(0) only works the second time I pressed it.
    You found an Solution?