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

Float to String C#

Discussion in 'Scripting' started by Gabe-Tucker, Dec 23, 2015.

  1. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    This is a relatively simple question. I created a UI that is supposed to show the health of the player. However, because the health of the player is a float value, I cannot use it to update the text of the UI, which is a string. Any suggestions on how to make this work? Thanks.
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    Code (CSharp):
    1. float myFloat = 10f;
    2.  
    3. string myString = myFloat.ToString();
     
  3. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Tried that already, no luck.

    Code (CSharp):
    1. public float totalHealth;
    2. public string displayHealth = totalHealth.ToString();
    Error: A field initializer cannot reference the nonstatic field, method, or property `lives.totalHealth'
     
  4. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    What about this?

    Code (CSharp):
    1. string myString = String.Parse(totalHealth);
     
  5. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    In that example, totalHealth has no value.

    You can also use something like:

    Code (csharp):
    1.  
    2. public string displayHealth = string.Format("{0}", totalHealth);
    3.  
     
  6. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Odd, it gives me the same error.
     
  7. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Do you mean I should do this which gives me an error saying: The name `String' does not exist in the current context
    public string displayHealth = String.Parse(totalHealth);
    or this, giving me the same error?
    public string displayHealth = displayHealth.Parse(totalHealth);
     
  8. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    I think I am thinking of the wrong thing... sorry...
     
  9. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    It's fine, thanks anyway,
     
  10. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    I may not be giving enough information. Here's the whole script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class lives : MonoBehaviour {
    5.     public float totalHealth;
    6.     public float damage;
    7.     public float minimumHealth;
    8.     public string displayHealth = totalHealth.ToString();
    9.     public GUIText health = displayHealth;
    10.  
    11.     void Start () {
    12.  
    13.     }
    14.  
    15.     void Update () {
    16.  
    17.     }
    18.  
    19.     void OnCollisionEnter2D (Collision2D collision)
    20.     {
    21.         if (collision.gameObject.tag == "Death Trap")
    22.         {
    23.             totalHealth -= damage;
    24.             health.text = totalHealth;
    25.         }
    26.         if (totalHealth <= minimumHealth)
    27.         {
    28.             Application.LoadLevel(Application.loadedLevel);
    29.         }
    30.     }
    31. }
    32.  
     
  11. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    try setting a default to totalHealth.

    Code (CSharp):
    1. public float totalHealth = 0f;
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    The error is because you are trying to call a method in your initialisation. Move the ToString call inside a method. Probably Update for UI stuff.
     
    Ryiah likes this.
  13. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Aha, I think you're on to something. I pinpointed the problem which has something to do with the GUIText health variable. I tried removing it, and the error goes away. Also, when I do what you suggested, I get new errors, both saying that I 'Cannot implicitly convert type `string' to `UnityEngine.GUIText'', pointing to line 24.
     
    Marlon3060 likes this.
  14. AssemblyBandit

    AssemblyBandit

    Joined:
    Dec 12, 2015
    Posts:
    12
    On line 24: health.text = ""+totalHealth;
     
    Last edited: Dec 24, 2015
    SmallLion likes this.
  15. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You probably want to ditch GUIText for the new UI system.

    But to solve your immediate problem you should be assigning the string to health.text instead of health.
     
  16. Gabe-Tucker

    Gabe-Tucker

    Joined:
    Nov 26, 2015
    Posts:
    94
    Horray, no more errors! Thanks for the help. Now I just have to figure out how to connect the variable with the text itself...
     
  17. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    Saved my life!Even after 5 years!
     
  18. PolyWorldGames

    PolyWorldGames

    Joined:
    Aug 27, 2020
    Posts:
    10
    1. public static float totalHealth ;
    2. public string displayHealth = totalHealth . ToString ();