public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
int abs = 29;
int z = player.position.z;
scoreText.text = (abs + z).ToString("0");
}
Answer from sunghee on Stack Overflowpublic Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
int abs = 29;
int z = player.position.z;
scoreText.text = (abs + z).ToString("0");
}
The solution should work for all negative numbers, not just -29 (you don't want it to break if someone has a score of -30, or -58, etc.). A general purpose solution to this problem could work like this:
using System;
using UnityEngine;
using UnityEngine.UI;
public class Score : MonoBehaviour {
public Transform player;
public Text scoreText;
// Update is called once per frame
void Update()
{
scoreText.text = Math.Max(0, player.position.z).ToString("0");
}
}
Note that 'Math.Max' has overloads for all primitive types of numbers, such as int, uint, double, float, etc. Fortunately for us, the compiler is smart enough to recognize that ints can be converted to floats without data loss, but not the other way around, so it will treat the 0 as a float, call the appropriate Math.Max method, and build successfully.
c# - Unity: How to convert a float value to a 00:00 string? - Stack Overflow
Convert float to string - Scripting - Unity Discussions
Convert Float to a String - Scripting - Unity Discussions
Float to String C# - C# & .NET - Unity Discussions
Is there anyway to show a float or Int on Unity Text?
When i use GUI.Label it does not scale with the screen size and makes me frustrated.
Thanks<3
Instead of doing your own calculations you can use TimeSpan to convert to a time format. Input will need to be of type double:
double mainGameTimerd = (double)mainGameTimer;
TimeSpan time = TimeSpan.FromSeconds(mainGameTimerd);
string displayTime = time.ToString('mm':'ss");
This is the piece of code I usually use
//Calculate the time in minutes and seconds.
int minutes = (int)levelDuration / 60;
int seconds = (int)levelDuration % 60;
//Update the duration text.
durationText.text = minutes.ToString() + ":" + ((seconds < 10) ? ("0") : ("")) + seconds.ToString();
It seems like this should be really obvious but I'm having a hard time finding an answer. I'm trying to display a float as a string. Here's the behavior I want (in this example I'm displaying up to 2 decimal places):
float: 1 > string: "1"
float: 1.23456 > string: "1.23"
The issue I'm having is most of the methods I'm finding for rounding/truncating the float (I don't really care if it's rounded or truncated) force zeros into the string if there is no decimal. So in the above example it would format float: 1 > string: "1.00" which is not what I want.
Thanks in advance!
I want to convert the x pos to a string but if i do this t.position.x.ToString();
sometimes there is a -E or something like that in the number. What can i do that this doesn't happen?
something like .ToString("something to Prevent this from happening");
im currently using this code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using JetBrains.Annotations;
using Unity.VisualScripting;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Windows;
public class output : MonoBehaviour
{
public Text main_output;
public float value = float.Parse(TEST.playerName, CultureInfo.InvariantCulture.NumberFormat);
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
main_output.text = value.ToString();
}
}