Search Unity

transform.position, wierd "bug"

Discussion in 'Scripting' started by Hindi, Apr 20, 2015.

  1. Hindi

    Hindi

    Joined:
    Apr 14, 2013
    Posts:
    59
    I made this function that I use to pop up some buttons over and object when the player clicks on it.

    Code (CSharp):
    1.  
    2.     public virtual void popUp(Vector3 position)
    3.     {
    4.         setActive(true); //Set the canvas that contains the menu to "active"
    5.         Vector2 pos = Camera.main.WorldToScreenPoint(position);
    6.         Debug.Log(pos);
    7.         menu.transform.position = pos;
    8.         Debug.Log(menu.transform.position);
    9.     }
    It is quite simple and works fine... Except for the very first call. When it is first called, the transform's position remains (0,0,0) event when the pos variable printed the correct position the line before.
    Anyone knows what can prevent the transform to update its position ?
     
  2. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    is "menu" a UI- Element?

    it really just a guess, but maybe it has to do that these UI-Element work with RectTransform instead of Transform

    GetComponent<RectTransform>().position
     
  3. Hindi

    Hindi

    Joined:
    Apr 14, 2013
    Posts:
    59
    Menu is indeed a UI-Element.
    I tried using Rectransform.anchoredPosition instead of transform.position but I had a constant offset and never figured out why, that's why I ended up using this solution which isn't the best.

    Still I find it wierd that the very first call fail and not the other, if using RectTransform.anchoredPosition solves the problem (and I'll give it another try), then I'd really like to understand why and how...