Function to call once a second
Hey! I have read some other answers, and I really don't know what I'm doing wrong with time.time =/. could anyone help me make this function call once a second?
void OnePerSecond ()
{
if (Time.time == timestamp)
{
timestamp = Time.time + 1.0f;
Debug.Log("a second passed");
if (secondsUntilShootBasic < 3)
{
secondsUntilShootBasic = secondsUntilShootBasic + 1;
}
}
}
Answer by Vylax · Mar 20, 2016 at 10:36 PM
Hi, your problem is you don't understand the code processing : when you write :
void OnePerSecond ()
{
if (Time.time == timestamp)
{
timestamp = Time.time + 1.0f;
Debug.Log("a second passed");
............
}
}
the script execute this : "if actual time is equal to "timestamp" so timestamp equal this time + 1 sec" and herés the problem : it instantly dtop the function so you have to use this code :
void OnePerSecond ()
{
if (Time.time == timestamp)
{
if (secondsUntilShootBasic < 3)
{
secondsUntilShootBasic = secondsUntilShootBasic + 1;
}
timestamp = Time.time + 1.0f;
Debug.Log("a second passed");
}
}
I think you're void would not be called evrey sec because you have to call this void manualy for it work: you have to write : "OnePerSecond();" in the Update void or call this void as Update:
void Update ()
{
if (Time.time == timestamp)
{
if (secondsUntilShootBasic < 3)
{
secondsUntilShootBasic = secondsUntilShootBasic + 1;
}
timestamp = Time.time + 1.0f;
Debug.Log("a second passed");
}
}
I hope it will work, tell me if you still have a problem, I would be able to completly resolve it tomorow on my computer because I'm actualy on phone and I'm sorry for my bad english ^^.
Hey!
Thanks for your answer, I ended up getting it to work with "InvokeRepeating("OnePerSecond",1,1) and then modifying how I did everything else. :D thank you so much for your answer though!
I'm happy to hear this. Plz close the question for set it as solved
Your answer

Follow this Question
Related Questions
Cycle NPC animations after certain time has passed 0 Answers
How make: Timer with Time.time 1 Answer
Invoke() does not delay my game timer 0 Answers
timer stopwatch 2 Answers