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 do I Round a transform.position.y from its float to the nearest whole int?

Discussion in 'Scripting' started by alexb2008, Mar 20, 2022.

  1. alexb2008

    alexb2008

    Joined:
    Mar 16, 2022
    Posts:
    34
    I am trying to make a points system based on the player's y position

    Points = transform.position.y;
    Points = Math.Round(Points);
    GUI.Label(new Rect(10, 10, 100, 20), "Level : " + Points);

    That is what google said was the soloution. It doesn't work.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    What does not work? What is the expected behavior? What happens instead?
     
    Bunny83 likes this.
  3. alexb2008

    alexb2008

    Joined:
    Mar 16, 2022
    Posts:
    34
    I was hoping this code would change the float of the player's position to a full integer which would be stored in the variable Point. all I get is an error messsage. It says that Math doesn't exist in the current context.
     
  4. D12294

    D12294

    Joined:
    Oct 6, 2020
    Posts:
    81
    Use Mathf instead of Math.
     
    alexb2008 likes this.
  5. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,920
    Just so you know, GUI.Label and the whole code-based GUI system is pretty old and no one ever liked it. The new "canvas" system allows you to just drag in a label (text), like a gameObject. It just says what it says and can be changed with (more-or-less) label1.text="new value";