Function not reseting variable passed to it

I have written a simple function to regulate the rate of fire for a ranged weapon. It works as long as I use a single, predeclared variable as a counter between two bursts, but as soon as I use two separate variables (for primary and secondary attack), it stops working. The variables are still used in an if statement, but it is not reset to zero later.

This is the function:

function fire(projectile : Transform, waitProj : int){								//FIRE!!!!
    if(waitProj >= projectile.GetComponent(Projectile).rateOfFire) {
	    waitRate++;																	//increase updates since last projectile
	    if(waitRate >= Mathf.Floor(statics.turnDuration / projectile.GetComponent(Projectile).projSpeed)) {
		    var tempProj = Instantiate(projectile, lookWP + Vector3(0, 5, 0), Quaternion.LookRotation(lookWP - oldWP));
		    tempProj.GetComponent(Projectile).owner = "player";
		    waitRate = 0;
	    }
	    if(statics.currentDuration == statics.turnDuration) {
		    waitProj = 0;
		    waitRate = 0;
	    }
    }
}

And this is the call (in FixedUpdate()):

fire(primAtt, waitPrim);

‘waitPrim’ is increased outside of the function and it counts the number of turns passed since the last burst. ‘waitRate’ counbts the number of updates since the last projectile.

When I use a single variable (waitProj) for both fire modes, it is set to 0 in the function, but I want the two fire modes to be independent of each other, so I declared two variables (waitPrim and waitSec) and pass them to the function instead, but they are not set to 0.

The variables are passed correctly, because the weapon is not fired, when I start the game, until the counter reaches the rateOfFire variable I set in the projectile, but then it is fired continuously, without pause, since the counter keeps counting up without ever being reset.

Any ideas, what I did wrong? Probably something fundamental…

After thinking about it some more, I think I know what the problem is, and as I thought, it is quite fundamental.

A function cannot change the variable passed to it, only the local variable it used within its own scope. If I want to get the changed value from a function, I need to either use predeclared variables or use ‘return’. Kinda makes sense…

Can someone, who has some more programming experience than me (i.e. more than two weeks), confirm this?

It looks like you need to pass waitProj as a ref. the ref keyword causes changes to the parameter to affect the original variable. You can pass a reference type or a value type by reference (ref).

void fire ( Transform projectile , ref int waitProj) {
    //.....
    //that will make this line work as expected.
    waitProj = 0;
    //......
}