Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Converting int to UI.Text [SOLVED]

Discussion in 'Scripting' started by BrodiMAN, Jul 21, 2017.

  1. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    82
    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:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class LifeDisplay : MonoBehaviour {
    7.  
    8.     Text lifeDisplay;
    9.     int count;
    10.  
    11.     void Start ()
    12.     {
    13.         lifeDisplay = GetComponent<Text>();
    14.         count = GameController.GetPlayerLife();
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.         lifeDisplay = count.ToString();
    20.     }
    21. }
    22.  
    Simple right? Well, Unity keeps calling me mean names for trying this. What am I doing wrong?

    Thanks for the help!
     
    Rsrc10 and puuloo like this.
  2. GamerJoHo

    GamerJoHo

    Joined:
    Oct 29, 2016
    Posts:
    17
    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.
     
    ColdTurkeyy, kx7787, puuloo and 4 others like this.
  3. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Try this:

    Code (CSharp):
    1. lifeDisplay.text = count.ToString();
     
    Carroot, puuloo, pan19652 and 2 others like this.
  4. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    82
    Wonderful!

    Thank you dudes for the help!
     
    puuloo likes this.
  5. Callum_Coombs

    Callum_Coombs

    Joined:
    Feb 26, 2019
    Posts:
    10
    This wasn't exactly my problem, as I wasn't using the .ToString thing, but it solved my issue! so thank you! :)
     
  6. arssarath23

    arssarath23

    Joined:
    Nov 26, 2021
    Posts:
    10
    GameController show error how to set it...?
    Please help me