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

Mathf.Floor not working.

Discussion in 'Scripting' started by Hero101, Jun 20, 2016.

  1. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    Everything is working in my local leaderboard script as intended. I realized I didn't want a long ass float displayed for the player's time in the game so I tried Mathf.Floor but it doesn't work when I call the saveTime function (from another script when level ends). Any idea why it still returns a float? For the record I tried Mathf.FloorToInt (something I don't really want to do if I can help it) and it still returned a float.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class TimeManager : MonoBehaviour {
    6.  
    7.     public Text counterText;
    8.     public float time;
    9.     private bool endTime;
    10.  
    11.     void Start ()
    12.     {
    13.         endTime = true;
    14.     }
    15.    
    16.     void Update ()
    17.     {
    18.         if (endTime)
    19.         {
    20.             time = Time.timeSinceLevelLoad;
    21.             counterText.text = time.ToString ("0");
    22.         }
    23.     }
    24.  
    25.     public void saveTime ()
    26.     {
    27.         endTime = false;
    28.         Mathf.Floor(time);
    29.         Debug.Log(time);
    30.         PlayerPrefs.SetFloat("PlayerTime", time);
    31.         PlayerPrefs.Save();
    32.     }
    33. }
    34.  
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,376
    Because you pass time into Mathf.Floor, but you don't assign the output of Mathf.Floor to anything.

    Code (csharp):
    1.  
    2. time = Mathf.Floor(time);
    3.  
    Mathf.Floor doesn't modify the input directly, it instead returns a new float that has been floored.
     
    Hero101 likes this.
  3. Hero101

    Hero101

    Joined:
    Jul 14, 2015
    Posts:
    158
    So simple it hurts.... Thanks buddy I appreciate the help.