Inspector giving different pos y then actual pos y?

Hello, im new to unity and am just trying to get my feet wet and make some basic game elements.

One such element that I am currently working on is a little UI menu that lets the player move and select options within it. To do this I wrote a small simple script to just move the cursor up and down between the possible selections. I used the cursors local y positions (as it is a child of the box).

But the cursor was acting strangely, so while debuging I asked it to print out its local pos y and its global pos y. Both of which were different than the pos y i see in the inspector.

Im sure that I am missing something simple and showing off my noobishness, but I am not sure what.

any help would be appreciated, thanks.

my code

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class DialogChoiceManager : MonoBehaviour {

    public GameObject ChoiceManager;
    public Text option1;
    public Text option2;
    public Text option3;
    public Text option4;
    public Image arrow;
    public bool isChoiceBoxOpen;


    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {

        // this controls the pointer in the dialogchoice box
        // we first check to make sure the box is even open
        if (isChoiceBoxOpen == true)
        {
            // check to see if we are at the top of the box.
            if (arrow.rectTransform.localPosition.y < 19.0f)
            {
                option1.text = arrow.transform.localPosition.y.ToString();
                option2.text = arrow.transform.position.y.ToString();

                // if we are not at the top of the box and the up key is pressed
                if (Input.GetKeyUp(KeyCode.UpArrow))
                {
                    // move the arrow up 13 units
                    arrow.rectTransform.localPosition = new Vector2(arrow.rectTransform.localPosition.x, arrow.rectTransform.localPosition.y + 13.0f);
                }
            }
          
            // check to see if we are at the bottom of the box.
            if (arrow.rectTransform.localPosition.y > -20.0f)
            {
                // if we are not and the down key is pressed
                if (Input.GetKeyUp(KeyCode.DownArrow))
                {
                    // move the arrow down 13 units
                    arrow.rectTransform.localPosition = new Vector2(arrow.rectTransform.localPosition.x, arrow.rectTransform.localPosition.y - 13.0f);
                }
            }
        }

    }




}

screenshot of the problem

RectTransform is a bit odd in that it is used for 2D-ish elements with UI-style layout qualities, but derives from Transform, which is predominately designed with 3D in mind, and with no notion of UI layouts. This is a result of proper 2D being added to the engine many years after it had started as a purely 3D engine. Thus, some of the properties like position and localPosition can be a little awkward.

Look through the properties added by RectTransform and maybe one of them will be the value that you’re actually looking for. I can’t recall for sure, but anchoredPosition might match what’s in the inspector.

2 Likes

Thanks that was in fact the value I was looking for and now everything is showing the correct numbers.