Search code examples
c#translationunityscripttimedelta

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();
}

Solution

  • because score is a float, i had to cast it to an integer before displaying it and the decimals are stripped off.

    here is the code that did the trick:

    score -= 60 * Time.deltaTime;
        guiText.text = "Score: " + (int) Score;
    

    and i also had to do timescale * 60 to get the same countdown speed as before. I don't really understand why it works this way but i'm happy with the result.