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

ScreentoWorldPoint not working for UI Object?

Discussion in 'Scripting' started by FalconJ, Jan 26, 2016.

  1. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    I have this code:

    Code (CSharp):
    1.    
    2. private GameObject draggedObject = null;
    3. void Update () {
    4.         if(Input.GetMouseButton(0))
    5.         {
    6.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    7.  
    8.             if(hit.collider)
    9.             {
    10.                 if(draggedObject == null)
    11.                 {
    12.                     Debug.Log("hello");
    13.                 }
    14.             }
    15.         }
    16. }
    The problem is that the mouse seems don't register the collider while clicking object under UI Canvas.
    If I put those objects outside canvas, then it work.

    How to fix this?

    Thank you
     
  2. jtsmith1287

    jtsmith1287

    Joined:
    Aug 3, 2014
    Posts:
    787
    Does the object you're trying to click have a collider?
     
  3. FalconJ

    FalconJ

    Joined:
    Mar 12, 2015
    Posts:
    146
    Yes
     
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Have you tried this tutorial?



    I used to use this, but I now use TouchScript from the Asset Store. It's really really good. Would defo suggest it :).