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

how can i round my score?

Discussion in 'Scripting' started by ratg97, Aug 7, 2014.

  1. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    i want round the score:



    the code is here:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ActualizarValoresGameOver : MonoBehaviour {
    5.  
    6.     public TextMesh total;
    7.     public TextMesh record;
    8.     public ControladorPersonaje distanceTraveled;
    9.  
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.      
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.      
    19.     }
    20.  
    21.     void OnEnable(){
    22.         total.text = distanceTraveled.distanceTraveled.ToString();
    23.         if(EstadoJuego.estadoJuego!=null){
    24.             record.text = EstadoJuego.estadoJuego.puntuacionMaxima.ToString();
    25.  
    26.         }
    27.     }
    28. }

    and the gui manager

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class GUIManager : MonoBehaviour {
    6.  
    7.  
    8.     public TextMesh distanceText;
    9.     private static GUIManager instance;
    10.     //private static GUIManager instance;
    11.  
    12.     void Start () {
    13.         instance = this;
    14.  
    15.     }
    16.        
    17.     public static void SetDistance(float distance){
    18.         instance.distanceText.text = distance.ToString("f0");
    19.     }
    20. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Mathf.Round and Mathf.RoundToInt will help you here.
     
  3. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    So yeah like StarManta said it would be:
    MyValue = Mathf.Round(InsertValueyouWantToRoundHere);
     
  4. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    but can you re edit my code?
     
  5. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    So the score value is distanceTraveled.distanceTraveled?
     
  6. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    And is it stored as a float?
     
  7. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.    
    4.     public class ActualizarValoresGameOver : MonoBehaviour {
    5.    
    6.         public TextMesh total;
    7.         public TextMesh record;
    8.         public ControladorPersonaje distanceTraveled;
    9.    
    10.    
    11.         // Use this for initialization
    12.         void Start () {
    13.        
    14.         }
    15.    
    16.         // Update is called once per frame
    17.         void Update () {
    18.        
    19.         }
    20.    
    21.         void OnEnable(){
    22.             total.text = Mathf.Round(distanceTraveled.distanceTraveled).ToString();
    23.             if(EstadoJuego.estadoJuego!=null){
    24.                 record.text = Mathf.Round(EstadoJuego.estadoJuego.puntuacionMaxima).ToString();
    25.    
    26.             }
    27.         }
    28.     }
    29.  
    Try that
     
  8. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    close.... i get it
     
  9. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    Good! So you think you can do it now?
     
  10. ratg97

    ratg97

    Joined:
    Aug 4, 2014
    Posts:
    65
    yesssss
     
    Aidenjl likes this.
  11. SpookyCat

    SpookyCat

    Joined:
    Jan 25, 2010
    Posts:
    3,679
    Or you could just use the ToString("0") method so the when the float is converted it only converts the full digits not any decimal places.
     
  12. Aidenjl

    Aidenjl

    Joined:
    Jan 5, 2014
    Posts:
    81
    I didn't even know that was a thing XD