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

Using RectTransformUtility.ScreenPointToLocalPointInRectangle in js [solved]

Discussion in 'Scripting' started by Doyora, Feb 5, 2016.

  1. Doyora

    Doyora

    Joined:
    Feb 2, 2013
    Posts:
    12
    I'm trying to make this function work in js, but I'm getting an error with the last argument of the function. Here is my code:
    Code (JavaScript):
    1.  
    2.     var point : Vector2;
    3.     print(RectTransformUtility.ScreenPointToLocalPointInRectangle(timelineObj, Input.mousePosition, null, point));
    I get the error "The best overload for the method 'UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(UnityEngine.RectTransform, UnityEngine.Vector2, UnityEngine.Camera, ref UnityEngine.Vector2)' is not compatible with the argument list '(UnityEngine.GameObject, UnityEngine.Vector3, null, UnityEngine.Vector2)'.".

    I have tried putting "out" before the point variable but then it tells me "expecting ), found 'point'.".
    The documentation does not give an example of how to use this function in js, so I am confused as to what I am doing wrong.
     
    Last edited: Feb 5, 2016
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Not out, but ref. Like ScreenPointToLocalPointInRectangle(timelineObj, Input.mousePosition, Camera.main, ref point);
    Most likely it sets values of ref variable, so act accordingly.
     
  3. Doyora

    Doyora

    Joined:
    Feb 2, 2013
    Posts:
    12
    Hi, thanks for the reply. I tried what you suggested but it still gives me an unexpected token error at the word "point".
     
  4. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    Ohhh riiight. The problem was different. It's not like you need ref or out(UnityScript doesn't have them); You're supplying first argument a GameObject instead of expected RectTransform.
    So it should look
    Code (JavaScript):
    1. var rectTransform = timelineObj.GetComponent(RectTransform);
    2.     var point : Vector2;
    3.     UnityEngine.RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, Input.mousePosition, Camera.main, point);
    At least that compiles for me. You might want to check if(rectTransform==null) and do something like error or not doing anything then.
     
  5. Doyora

    Doyora

    Joined:
    Feb 2, 2013
    Posts:
    12
    Ahhh yeah there we go. works great now, Thankyou!