Converting A Float to a INT Countdown Timer.... C#

Keep having an issue, with converting this GUI to display in INT rather then in a float format… and it looks ugly at the minute… :slight_smile:

As per - help would be appreciated!

using UnityEngine;
using System.Collections;

public class PlayerAdrenaline : MonoBehaviour {

public int MaxAdrenaline = 100;
public int CurrentAdrenaline = 0;
public float AdrenalineBarLength;

public float TimeSlowFactor = 0.2f;
public float TimeSlowDuration = 1f;
public int CountDown = 0f;

	// Use this for initialization
	void Start () 
	{
		AdrenalineBarLength = Screen.width / 2;
	}
	
	// Update is called once per frame
	void Update () 
	{	

		CountDown += Time.deltaTime*4;

not a c# peep… but don’t you just need to cast it as an Int?

http://msdn.microsoft.com/en-gb/library/ms173105(v=vs.80).aspx (casting)

1 Like

I can’t even see where you have tried…

but you cast it, like (int)float

ie:

(int)CountDown;
1 Like

Ive tried this guys; i’ve editted my code above; the problem i’m having is is cannot implicity convert type float to int for my Countdown to Time.delta time. section…

 CountDown += (int)Time.deltaTime*4;

Cheers,

I did that but then my counter refused to count up anymore :\ it just stayed at 0.

bump!

Why are you multiplying it by 4? You could just use ToString(“f0”) and that would cause your float to only display the first number,

CountDown += Time.deltaTime;
Debug.Log("Countdown: " + CountDown.ToString("f0");
3 Likes

fIXED WITH!

CountDownFloat += Time.deltaTime*4;
CountDown = Mathf.floor(CountDownFloat);

and

Mathf.FloorToInt

DexRobinson, thanks. That fixed my issue.

thanks robinson

thx man after 4 years it have help me still :smile:
but i have use : Mathf.RoundToInt(seconds);

1 Like