Daniel Lip
Daniel Lip

Reputation: 11325

How can i display on the game window a variable float values in real time as text for testing?

When running the game i want to display in real time some stats and info. For now i want to display the landingSpeed variable value on the game window. In the left top corner.

But it's not showing anything. I want to display it in the top left corner and to be able to set the text font size in the script.

using UnityEngine;
using System.Collections;

public class MakeObjectFly : MonoBehaviour
{
    public float AmbientSpeed = 100.0f;
    public float RotationSpeed = 200.0f;
    public float flightSpeed = 5.0f;
    public float landingSpeed = 100.0f;
    public bool isGrounded = true;

    Rigidbody _rigidbody;

    public UnityEngine.UI.Text displaylandingspeed;

    void Start()
    {
        _rigidbody = GetComponent<Rigidbody> ();
    }


    void Update ()
    {
        Quaternion AddRot = Quaternion.identity;
        float roll = 0;
        float pitch = 0;
        float yaw = 0;
        roll = Input.GetAxis ("Roll") * (Time.deltaTime * RotationSpeed);
        pitch = Input.GetAxis ("Pitch") * (Time.deltaTime * RotationSpeed);
        yaw = Input.GetAxis ("Yaw") * (Time.deltaTime * RotationSpeed);
        AddRot.eulerAngles = new Vector3 (-pitch, yaw, -roll);
        _rigidbody.rotation *= AddRot;
        Vector3 AddPos = Vector3.forward;
        AddPos = _rigidbody.rotation * AddPos;
        if (isGrounded == true) {
            landingSpeed = Input.GetAxis ("LandingSpeed") * (Time.deltaTime * AmbientSpeed);
            _rigidbody.velocity = AddPos * (Time.deltaTime * AmbientSpeed + landingSpeed);
            if (landingSpeed == 100)
                landingSpeed = 0.0f;
            displaylandingspeed.text = landingSpeed.ToString ();
        }
    }
}

I added the variable displaylandingspeed but it's not showing it at all when running the game.

Two screenshots the first showing the Text Inspector. The second showing the GameObject with the script and the Text attached to it.

Text1

Text2

Upvotes: 0

Views: 135

Answers (1)

Anmol Mahatpurkar
Anmol Mahatpurkar

Reputation: 561

The text object that can be seen in the screenshot is not a child of a Canvas. Any object with the Unity UI components should be a child of a Canvas Object. Only then will it be rendered on the screen.

Make your text object a child of the Canvas object, and the problem should be solved.

Upvotes: 1

Related Questions