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 /
This question was closed Oct 14, 2013 at 03:47 PM by clunk47 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BradleyBeeke · Oct 12, 2013 at 02:18 PM · c#gui

Timer Help C# - Displaying it in a non Decimal format

Hi i have this script that will turn a directional light around to emulate a sun that is all working fine but i want to display the time on a gui label that i can do aswell the thing i cant do is to stop it from being a decimal when i run it it works fine, i want it to be a whole number like 1 instead of 1.11032046520 please tell me how i can fix this.

My Code-

 //this is the cut version because the real one is very long 
 //this code includes the parts of the .cs i am having trouble with
 // first the int declare which is the game time
 int currentCycleTime (int);
 
 // now for the OnGUI()
 Void OnGUI()
 {
    //show the current time as a gui text
    GUI.Label(new Rect(10,10,100,20), "Time:"+ currentCycleTime );
    print (currentCycleTime);
 }
Comment
Add comment · Show 19
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 vexe · Oct 12, 2013 at 02:23 PM 1
Share

You could downcast it to an int. int n = yourDecimalNumber;

avatar image vexe · Oct 12, 2013 at 02:32 PM 1
Share

Yes, downcasting it to an int will return you a whole number.

 int n = 1.11032046520;
 print (n); // 1
avatar image vexe · Oct 12, 2013 at 02:40 PM 1
Share

Pff, dear compiler ~_~ that's an obvious implicit cast.

Anyway, seems that he doesn't like it. Do an explicit cast:

 int n = (int)yourDecimalNumber;
avatar image vexe · Oct 12, 2013 at 03:56 PM 1
Share

Sorry for the delay but I don't get notified of your edit.

You seem to be new to both program$$anonymous$$g and Unity. Please work on your program$$anonymous$$g skills before you jump into Unity, cause you won't go that far :(

First thing, it's void and not Void, make sure you correct that.

Then, about your timer thing, you said that you're using a float variable, and that you're getting it in a decimal way. I suggested that you downcast it to an int, via int n = (int)yourDecimalNum; - I see that you are mis-using this with your int currentCycleTime - you should stick to your float/decimal value, and then when you want it to appear as a whole number just downcast it there.

One compiler error is the (int) after your variable declaration.

 int currentCycleTime (int); // remove the "(int)"

So as I said, just stick to your decimal number idea, and then:

 GUI.Label(new Rect(10,10,100,20), "Time:"+ (int)currentCycleTime );

or perhaps you need the whole value somewhere else, store it in a variable:

 // ...
 int whole = (int)currentCycleTime;
 GUI.Label(new Rect(10,10,100,20), "Time:"+ whole);

Let me know how it goes :)

avatar image BradleyBeeke · Oct 12, 2013 at 04:06 PM 1
Share

the only error is CS0103: the name 'currentCycleTime' does not exist in the current context. i need the name in there because there is about 700 lines of code and they all need the name currentCycleTime to function

Show more comments

1 Reply

  • Sort: 
avatar image
4
Best Answer

Answer by vexe · Oct 12, 2013 at 04:37 PM

It depends on what you really need. print does a Debug.Log which prints stuff to your console. I don't think you even need it in this context cause you're already seeing the value from your gui. Just mess around with it man. Anyway, you could try:

 int whole = (int)currentCycleTime; // ASSUMING that you rolled back to your decimal/float version
 GUI.Whatever(stuff, stuff, stuff, whole);
 print (whole);
 
 // ANOTHER APPROACH (same effect):
 GUI.Whatever(stuff, stuff, stuff, (int)currentCycleTime);
 print ((int)currentCycleTime);

obviously the 2nd approach is redundant since you're casting twice.

Comment
Add comment · Show 1 · 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 vexe · Oct 12, 2013 at 05:00 PM 1
Share

@BradleyBeeke: Don't forget to give it the tick ;) - And if a comment I gave was useful to you, give it the thumb ;) - I appreciate it, it's your way of saying thanks :)

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Show GUI.Label when touching trigger 4 Answers

How to disable my Script and Gui 1 Answer

texture and text on gui 1 Answer

Button position 4 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