Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Danor · Sep 18, 2014 at 07:40 PM · c#time.deltatime

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();
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bluntweapon · Sep 18, 2014 at 08:10 PM 0
Share

Or, you know, change it to an int, like it was in the US script.

avatar image Danor · Sep 18, 2014 at 10:03 PM 0
Share

@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)

avatar image dmg0600 · Sep 18, 2014 at 10:04 PM 1
Share

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;
avatar image Danor · Sep 18, 2014 at 10:18 PM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges