- Home /
Why Won't This Message Log Into My Console?
I am just testing out a simple timer that will provide some later logic in my game. I want this message to log into my Unity debug console whenever 30 seconds has passed in the scene, but it is not happening... Am I going crazy or did I make some stupid silly mistake??
using UnityEngine;
using System.Collections;
public class TestingTimer : MonoBehaviour
{
private float startTime;
void Start ()
{
startTime = Time.timeSinceLevelLoad;
}
void Update ()
{
float currentTime = Time.timeSinceLevelLoad - startTime;
if (currentTime == 30f)
{
Debug.Log("Time is 30!!");
}
}
}
Answer by doublemax · Oct 15, 2016 at 08:39 PM
timeSinceLevelLoad moves in small fractions of a second. The chance that currentTime becomes exactly 30 is very slim.
But you could use:
if ( (int)currentTime == 30 )
It's close enough for me! I thought that all numbers stored inside the "Time" class were floats, so I never thought to cast as an Integer. Thank you sir.
Your answer
Follow this Question
Related Questions
I want to have cancelinvoke start multiple seconds arfter object becomes invisible 1 Answer
Distribute terrain in zones 3 Answers
Timer counting UP instead of DOWN 1 Answer
How to work a real life timer? 2 Answers
How to make a Time Based Score? 1 Answer