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

Trying to move an InputField object around when held and dragged around

Discussion in 'Scripting' started by hazem_inspectar, Jan 16, 2020.

  1. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7
    Hello,

    I have a script that's supposed to detect when the mouse is being held down (TextbeingHeld) and pan around an inputField text box. The script finds the current position of the mouse pointer, then updates the position of the inputField that has an EventTrigger. However, the code doesn't seem to work properly.

    The commented code is another method I tried using to find the x and y positions of the pointer but I am getting a null reference when I'm in game when TextbeingHeld becomes true.

    Any help would be greatly appreciated.



    Code (CSharp):
    1. public class textBoxPointer : MonoBehaviour
    2.     {
    3.         private float speed = 10000.0f;
    4.         private Vector2 target;
    5.         private Vector2 position;
    6.         private Camera cam;
    7.         private bool TextbeingHeld = false;
    8.  
    9.         void Start()
    10.         {
    11.             target = new Vector2(554f, 297f);
    12.             position = gameObject.transform.position;
    13.  
    14.             cam = Camera.main;
    15.         }
    16.  
    17.         void Update()
    18.         {
    19.             Debug.Log(TextbeingHeld);
    20.             float step = speed * Time.deltaTime;
    21.             // move sprite towards the target location
    22.             transform.position = Vector2.MoveTowards(transform.position, target, step);
    23.             if (TextbeingHeld)
    24.             {
    25.                 MoveText();
    26.             }
    27.         }
    28.  
    29.         public void holdDown()
    30.         {
    31.             TextbeingHeld = true;
    32.         }
    33.  
    34.         public void holdUp()
    35.         {
    36.  
    37.             TextbeingHeld = false;
    38.  
    39.         }
    40.  
    41.         public void MoveText()
    42.         {
    43.             Event currentEvent = Event.current;
    44.             //Vector2 mousePos = new Vector2();
    45.             Vector2 point = new Vector2();
    46.  
    47.             // compute where the mouse is in world space
    48.  
    49.             var mousePos = Input.mousePosition;
    50.             //mousePos.x = currentEvent.mousePosition.x;
    51.             //mousePos.y = currentEvent.mousePosition.y;
    52.             float x = Input.mousePosition.x;
    53.             float y = Input.mousePosition.y;
    54.             point = cam.ScreenToWorldPoint(new Vector3(x, y, 0.0f));
    55.  
    56.                 target = point;
    57.            
    58.         }
    59.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Please be a little more specific than this.

    - which script
    - which line
    - why you think it should NOT be a null ref

    And if there is more than one thing dereferenced on the line, rewrite it into multiple lines so you actually know which variable is null.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
  4. hazem_inspectar

    hazem_inspectar

    Joined:
    Jan 10, 2020
    Posts:
    7

    I changed my code to the following but I am still not able to move around the InputText field. It should theoretically move I don't see the errors. I fixed the null references.

    Code (CSharp):
    1. public class textBoxPointer : EventTrigger
    2.     {
    3.         private float speed = 10000.0f;
    4.         private Vector2 target;
    5.         private Vector2 position;
    6.         private Camera cam;
    7.         private bool TextbeingHeld = false;
    8.  
    9.         void Start()
    10.         {
    11.             target = new Vector2(554f, 297f);
    12.             position = gameObject.transform.position;
    13.  
    14.             cam = Camera.main;
    15.         }
    16.  
    17.         void Update()
    18.         {
    19.             float step = speed * Time.deltaTime;
    20.             transform.position = Vector2.MoveTowards(transform.position, target, step);
    21.         }
    22.  
    23.         public void holdDown()
    24.         {
    25.             TextbeingHeld = true;
    26.         }
    27.  
    28.         public void holdUp()
    29.         {
    30.  
    31.             TextbeingHeld = false;
    32.  
    33.         }
    34.  
    35.         void OnGUI()
    36.         {
    37.             Event currentEvent = Event.current;
    38.             Vector2 mousePos = new Vector2();
    39.             Vector2 point = new Vector2();
    40.  
    41.             // compute where the mouse is in world space
    42.             mousePos.x = currentEvent.mousePosition.x;
    43.             mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;
    44.             point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 0.0f));
    45.  
    46.             if (TextbeingHeld)
    47.             {
    48.                 Debug.Log(TextbeingHeld);
    49.                 target = point;
    50.             }
    51.            
    52.         }
    53.     }
     

    Attached Files: