- Home /
The question is answered, right answer was accepted
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);
}
Yes, downcasting it to an int will return you a whole number.
int n = 1.11032046520;
print (n); // 1
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;
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 :)
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
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.
@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
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