Adding some amount to float/int variable PER SECOND! C#

hi, I’m using C#
I have situation like this:
player can shoot bullets each player has stats that it can upgrade like (bullet range, bullet speed, bullet dmg, RELOAD TIME)

problem is in reload time;
float reload (current reload time)
float maxreload (number that “reload” should be to enable shooting)
when bullet is triggered, reload time drops on 0, if (reload < maxreload) then I want to make reloading increasing for 1 EACH SECOND

public float maxreload = 3;
public float reload;

void Start()
{
	reload = maxreload;
}

void Update()
{
	if(Input.GetKeyDown(KeyCode.Mouse0)  reload == maxreload) 
	{
		Network.Instantiate(bullet, transform.position, transform.rotation, 0);
		reload = 0;
	}
	if(reload < maxreload)
	{
		[COLOR="red"]reload++;[/COLOR]
	}
}

everything works fine except I don’t know how to make reloading increase per second
i have tried:

reload = reload + Time.deltaTime;

but that doesn’t works, please tell me how to do this
it shouldn’t be hard…

public float maxreload = 3;
public float reload;

void Start()
{
	reload = maxreload;
}

void Update()
{
	if(Input.GetKeyDown(KeyCode.Mouse0)  reload == maxreload) 
	{
		Network.Instantiate(bullet, transform.position, transform.rotation, 0);
		reload = 0;
	}
	if(reload < maxreload)
	{
		StartCoroutine(Reload));
	}
IEnumerator Reload() {
yield return new WaitForSeconds(maxreload - reload  );

               reload = reload + 1;


        }
}

try this

You have to increment reload with Time.deltaTime because this is the time that has elapsed since the last update() cycle. By adding this to your reload variable, it will hold the total time that has passed since the last shot.

However …

it is probably not working at your side because you are comparing it with maxreload and the chance that reload will be exactly the same is very small indeed!

so try to compare it like … reload >= maxreload, and it will give you joy!

You can try this …

void Update()
{
	if(Input.GetKeyDown(KeyCode.Mouse0)  reload >= maxreload) 
	{
		Network.Instantiate(bullet, transform.position, transform.rotation, 0);
		reload = 0;
	}
	if(reload < maxreload)
	{
		reload+=Time.deltaTime;
	}
}

Kind regards,

Better to use coroutines for this sort of thing anyway (but don’t try to mix them with Update; rokstar234’s code won’t work because it will start a new coroutine every frame):

public GameObject bullet;
public float reloadTime = 3;

IEnumerator Start() {
	while (true)
	{
		if (Input.GetMouseButtonDown(0))
		{
			Network.Instantiate(bullet, transform.position, transform.rotation, 0);
			yield return new WaitForSeconds(reloadTime);
		}
		yield return null;
	}
}

–Eric