- Home /
Segment within a nested If does not get evaluated
I'm trying to do a multicounter script to keep track of time. So far, it just counts simulated seconds, minutes and hours using a coroutine and some nested if-else instructions. I've added an extra if-else structure within the minutes section to count degrees, assuming it will increment or decrement one degree per minute, yet this part of the code is as if it were invisible, for the compiler does not see it.
public class MultiCounter : MonoBehaviour {
public bool process= true;
public bool subprocess = true;
public float timerSpeed = 0.125f;
public int iSeconds, iMinutes, iHours;
public int iDegrees = 0;
// Use this for initialization
void Start () {
StartCoroutine (TripleCounter ());
}
IEnumerator TripleCounter()
{
while(process)
{
yield return new WaitForSeconds(timerSpeed);
if(iSeconds >= 59)
{
iSeconds = 0;
if(iMinutes >= 59)
{
iMinutes = 0;
// Problematic code...
if(iDegrees >= 359)
{
iDegrees = 0;
}
else
{
iDegrees++;
}
if(iHours >= 23)
{
iHours = 0;
}
else
{
iHours++;
}
}
else
{
iMinutes++;
}
}
else
{
iSeconds++;
}
Debug.Log(iHours + ":" + iMinutes + ":" + iSeconds + " " + iDegrees + "°");
}
}
}
Seconds, minutes and hours get incremented correctly, but nothing happens with degrees... Even a Debug.Log after the line iMinutes = 0; does not get evaluated, yet the rest of the code related to iHours does indeed evaluate correctly.
Any hep would be appreciated.
Thank you.
Yes, Robertbu, that is precisely what I need the code to do. The problem is that iDegrees does not increment: for all purposes, the iDegree selection is like if it didn't exist.
Sorry, I don't understand your comment. If you set i$$anonymous$$inutes to 59, you will see iDegree increment to 1 as i$$anonymous$$inutes rolls over back to 0. I just tested to to make sure.
Answer by robertbu · Jul 26, 2014 at 04:56 PM
You've nested your iDegrees code inside your iMinutes code. That means that iDegrees will only be incremented as iMinutes rolls from 59 back to 0. To verify what I'm saying, add this to Start():
iMinutes = 59;
Then watch until iMinutes rolls over to 0. You will see iDegrees incremented. This may not be what you want, but this is what you wrote.
Answer by SilentSin · Jul 27, 2014 at 02:39 AM
Why are you using:
yield return new WaitForSeconds(0.125f);// assuming timer speed actually stays at that value.
When you appear to be trying to count seconds and minutes? Why not just yield for 1 second each time?
As you have it, iSeconds++ will be called every 0.125 seconds, meaning it goes up by 8 for every second that actually passes. If this is what you want, then why not change the yield to 1 second and use iSeconds += 8 each time. Or you could keep using timerSpeed, but use it as an actual speed (iSeconds += timerSpeed), so timerSpeed == 2 means its going twice as fast as real time and == 8 means 8 times as fast.
Hello SilentSin.
Time inside a game usually is faster than in real life. However, time scale should be consistent with what you have come to expect from reality. Imagine a diver jumps from a 20 meters cliff and has to perform several twists and turns before reaching the water. To couple some real physics with a game, your time scale has to be the correct one and since the game engine already quantitaze the time, adding a multiplier makes calculations more complex. In other words: you can halve the time by 0.5, 0.25, and 0.125 but if you use almost any other value, the results does not resemble real life (like being able to make a quadruple somersault jumping from your own height). But the moment you scale it up, things don't work... Imagine a character that stands 2 meters tall and weights 70 kilograms: when you scale it down it performs mostly as the original does because you've also scaled down any errors in your calculations; but if you scale it up you square them.
However, there is a limit for scaling down the WaitForSeconds instruction (about two tenths of a second) and any further scaling just produces garbage output, like ignoring part of the code -which was my original question. When speedTime is equal to 1 all instructions in the code are evaluated, but not when timeSpeed is below that barrier of two tenths of a second.
Your answer
![](https://koobas.hobune.stream/wayback/20220613155032im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Taking a sequence of screenshots 1 Answer
Starting Coroutine within Start method 1 Answer
Method returning data from WWW 1 Answer
How to stop coroutines or functions 2 Answers