How to Get a Variable to Increase?

Hi! I want my timer variable to increase by 1.5 every second. How do I do this?

If you have a float that is keepng track of time, then you either set up a invokerepeating to increment it every second by 1.5, or you use a coroutine with a loop that will wait for 1 sec and then increment it by 1.5. I suggest the coroutine version personally.

Hmm, isn’t there something where you can just do
timer += 1.5;
Or is that Game Maker stuck in my mind?

Of course, but you have to have a way of telling it when to do that. Otherwise, how would it know you want to add 1.5f every 1 second?

A coroutine does that by letting you declare a wait time, then add 1.5f to your timer. Then because you want it in a loop, it will loop and wait 1 sec before adding again.

timer +=1.5f will add the value once and nothing wrong with that line, just need to include the logic

I’d go with a Coroutine or invoke repeating for the convenience of it since it is a function which does a thing every time interval. You wouldn’t even need the variable to increase.

If you are still hell bent on using a variable instead, you could do this on Update. It may work but really isnt ideal.

public float timeInterval//would be set to 1.5 in the inspector
float currTime//the variable to use for whatever it is you are doing

void Update(){
    currTime += Time.deltaTime;
    if(currTime >= timeInterval){
        //do stuff
        currTime = 0;
    }
}