- Home /
Translating UnityScript to C#. time.delta displays differently
I'm working with a save/loadscript in c# and i want to save highscores. My scorecounter is in unityscript so i've translated the script to c# but the text is displayed differently and i don't like the way it is displayed now, it worked better for me using unityScript.
score starts at 10 000 and will run down, if score hits 0 player loses but if the player beats the level at 5000 score left, he gets 5000.
the unityscript writes score to guiText as: 10 000 the c# writes score to guiText as: 10 000.000
10 000 in unityscript should be 100 seconds. 10 000 in c# however is 10 000 seconds. i can put 100 instead of 10 000 in the c# one but it then counts like this 100.00000 -> 99.99999 it may count 100 just like the US does but the way it is displayed is unusable. here are both scripts (no errors): c#:
using UnityEngine;
using System.Collections;
public class ScoreManager : MonoBehaviour {
public float score;
private IEnumerator Wait() {
yield return new WaitForSeconds(3);
Application.LoadLevel(Application.loadedLevel);
}
void TimerOfDeath(){
if(score <= 0){
GameObject.Find("TooLateGUI").guiTexture.enabled = true;
GameObject.Find("Score").guiText.enabled = false;
StartCoroutine(Wait());
}
}
void Update () {
{
score -= Time.deltaTime;
//Score -= 1 * Time.deltaTime;
guiText.text = "Score: " + score;
TimerOfDeath ();
}
}
}
and the US:
var Score : int;
function Start(){
gameObject.Find("TooLateGUI").guiTexture.enabled = false;
}
function TimerOfDeath(){
if(Score == 0){
gameObject.Find("TooLateGUI").guiTexture.enabled = true;
gameObject.Find("Score").guiText.enabled = false;
yield WaitForSeconds(5.0);
Application.LoadLevel(Application.loadedLevel);
}
}
function Update ()
{
Score -= 1 * Time.deltaTime;
guiText.text = "Score: "+Score;
TimerOfDeath();
}
Answer by Slev · Sep 18, 2014 at 07:45 PM
Yes because score is a float, cast it to an integer before displaying it and the decimals will be stripped off.
guiText.text = "Score: " + (int) Score;
Or, you know, change it to an int
, like it was in the US script.
@slev thanks for replying mate, that works for displaying it right but if i enter 10 000 now it will still count 1 per second unlike the US script where 10 000 is 100. any idea's on how i could fix this too?
@bluntweapon that throws an error: Error CS0266: Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) (CS0266) (Assembly-CSharp)
Declaring it as int won't work as Time.deltaTime is a float.
If your sole purpose is to show the time without decimal part, casting it to int as Slev has mentioned will work as a charm. If you want to maintain the 10000 format you culd just multiply the deltaTime by 100 as follows:
score -= Time.deltaTime * 100;
guiText.text = "Score: " + (int)score;
yes you are right dmg0600, i did want to keep the 10000 format but i had to do time.deltatime * 60 to get same results, 100 seemed to be faster thanks guys
Answer by Danor · Sep 19, 2014 at 01:03 AM
score -= 60 * Time.deltaTime;
guiText.text = "Score: " + (int) score;
the 2nd line suggested by Slev worked and by doing 60 * time.deltatime i seem to get the same results as i get from the unityscript. I don't completely understand why but i'm happy with the result.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
color in textmesh 1 Answer
Having trouble increasing the spawning speed of explosions 0 Answers
After collision how the run animation to die animation will change.. 0 Answers