Search Unity

[ Solved ] Display transform.position.y in a Text element.

Discussion in 'AR/VR (XR) Discussion' started by Untoldecay, Nov 17, 2018.

  1. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    Hello,

    I'm trying to display the y position of my OVRCameraRig in a Text object for debugging purpose.
    I can get the y value ( debug.log display it correctly ), but impossible to display it inside my text element...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class ForForum : MonoBehaviour
    7. {
    8.     public Text debugText;
    9.     public float cameraHeight = 1.75f;
    10.     public Transform realCameraHeight;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         debugText = GameObject.Find("debugText").GetComponent<Text>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         debugText.text = "realCameraHeight = " + realCameraHeight.position.y.ToString();
    22.         Debug.Log("realCameraHeight = " + realCameraHeight.position.y.ToString());
    23.     }
    24. }
    So, I get this in my console : "realCameraHeight = 1.75"
    But this on my text element : "realCameraHeight ="

    What am I missing?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your code is correct. My guess is that your Text element is too short.
     
  3. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    @JoeStrout Thanks for giving my problem a try.
    I checked the size of the text container and it should not be the problem.
    The container is larger than the text itself.

    May the OVRCameraRig as already a bit of script messing with the y value?

    What's weird is that when I set
    cameraHeight = 0
    , the 0 appears correctly in my text element...
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It can't be anything messing with the Y value. realCameraHeight.position.y is a float, so there is no way that realCameraHeight.position.y.ToString() can produce a blank or empty string.

    Run the game, and then use the Inspector to inspect your Text element. What do you see in its text property?
     
  5. Untoldecay

    Untoldecay

    Joined:
    Apr 9, 2016
    Posts:
    24
    Well,
    now it display correctly the value in the text element & the inspector pan... weird.
    I changed things in my scripts but they were not supposed to be related to this...

    Weird weird weird.

    Now this is working, I will probably create an other thread for asking about my OVRCameraRigh's y which for a given value seems to be twice higher when playing the apk in my go...

    Thanks @JoeStrout !