- Home /
How do I get very high numbers to register within Unity?
I'm making a game where you climb stairs and earn points with each step - kind of like a clicker. I have it coded so that new dialogue appears when your score reaches specific amounts (Your score goes up using "onTriggerEnter")
So far it's worked flawlessly, but towards the end, the score gets really high, so Unity abbreviates the numbers to 1e+08. The last dialogue that appears is at the number 100,000,000, which it abbreviates to 1e+08, but when I try to get the dialogue to change at let's say, 100,001,000 (which it abbreviates to1.00001e+08), the new dialogue does not appear.
I think this is a Unity software question. as I've also noticed that if I change my score manually while the game is running in Unity, it turns 99,999,999 into 100,000,000 automatically.
Thanks!
If this helps:
{
public float currentMoney;
public float step = 100;
public TextMeshProUGUI moneyText;
void Update()
{
UpdateUI();
}
// Update is called once per frame
void UpdateUI()
{
moneyText.text = "Money: $" + currentMoney.ToString("N0") + ",000";
}
public void IncreaseScore()
{
currentMoney = (currentMoney + step);
UpdateUI();
}
}
Answer by Bunny83 · Apr 27, 2020 at 03:14 AM
Well, this is not really a Unity thing that's just how floating point numbers work. I recommend to have a look at my answer over here where I've posted a table to show how the precision changes with increasing numbers. Also this interactive Floating point converter is quite handy to better understand the underlaying structure.
Though apart from that I really recommend spending those 10 minutes to watch this Computerphile video on floating point numbers
Depending on your usecase if you only need to deal with whole numbers you might want to just use a long (64bit number). If a long isn't enough you can use the BigInteger class. Though that class is orders of magnitude slower than primitive types like int or long. Though for a clicker game using BigInteger isn't really a problem.
Ahh, I'm not used to dealing with such big numbers, thanks for the links, I'm checking them out now!
Answer by KiTS0 · Apr 27, 2020 at 03:19 AM
Here's an example of what I'm thinking of.
This is just to resolve the display of the counted stairs.
using System;
using UnityEngine;
using UnityEngine.UI;
public class CountStairs : MonoBehaviour
{
public Text stairCountTextField;
void Start()
{
//A UI > Text GameObject named CountToText //Gets Text component inside of CountToText
stairCountTextField = GameObject.Find("CountToText").GetComponent<Text>();
//Manual assignmnet into the text field.
//stairCountTextField.text = "test.";
}
//int has a limit of 2147483647
//long has a limit of 9223372036854775807
//See more at https://www.tutorialsteacher.com/csharp/csharp-data-types
public long step;
void Update()
{
stairCountTextField.text = Convert.ToString(step);
}
}
I changed "float" to "long" and it seems to have worked!
Thanks so much, I'll take a look at that tutorial!
Your answer
Follow this Question
Related Questions
Timer Runtime Problem 2 Answers
Can anyone help ?? Timer and gui 0 Answers
pickup counter and timer help 0 Answers
How can I get an accurate gameObject count within a frame? 1 Answer
My orb and gem counter are counting simultaneously? 1 Answer