Converting int to UI.Text [SOLVED]

I’ve seen this question asked a lot around here, but for some reason the answers vary depending on…things.

I’m simply trying to display the remaining player lives (int) on the HUD canvas as a UI.Text. Yet, Unity doesn’t agree with me on this. Please see my code below:

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

public class LifeDisplay : MonoBehaviour {

    Text lifeDisplay;
    int count;

    void Start ()
    {
        lifeDisplay = GetComponent<Text>();
        count = GameController.GetPlayerLife();
    }

    void Update ()
    {
        lifeDisplay = count.ToString();
    }
}

Simple right? Well, Unity keeps calling me mean names for trying this. What am I doing wrong?

Thanks for the help!

2 Likes

The issue’s not with ToString(), you need to get text of lifeDisplay.

So your Update function should read:
lifeDisplay.text = count.ToString();

Note… text maybe capitalised… I can’t remember and not near Unity at the moment.

7 Likes

Try this:

lifeDisplay.text = count.ToString();
5 Likes

Wonderful!

Thank you dudes for the help!

1 Like

This wasn’t exactly my problem, as I wasn’t using the .ToString thing, but it solved my issue! so thank you! :slight_smile:

GameController show error how to set it…?
Please help me