Simple Code Not Working

So I don’t know what I’m missing but I can’t get this to work. I basically need an object to jump every so often.

Here’s my code:

var counter : int = 1;

var power : float = 500.0;

function update () {
	if(counter <= 20){
	counter += 1;
	}
	if(counter > 20){
	rigidbody.AddForce(Vector3(0,power,0));
	counter = 1;
	}
	}

The counter won’t go up when I run it. I tried counter++ and that didn’t work either. Any help is appreciated!

function Update, not function update. This way, you have not declared a function that will be executed each frame, but a function that will never be executed :slight_smile:

Update() and update() are not the same function – note the capital ‘U’.

If you use this code, the amount of time between jumps will vary depending on the game’s current FPS; that’s usually not a desired behavior. You might instead consider using FixedUpdate() or InvokeRepeating() to manage this.

As @BiG said, update isn’t the same thing as Update - you should fix the function name (it’s Update).

Anyway, all you will get with this code is a rigidbody being accelerated to the sky - 20 frames isn’t a stable and useful time interval. You could instead set the next time to jump after each jump:

var jumpInterval: float = 1.0; // interval between jumps in seconds
var power : float = 500.0;
private var timeToJump: float = 0.0;

function Update () {
    if (Time.time >= timeToJump){
        rigidbody.AddForce(Vector3(0,power,0));
        timeToJump = Time.time+jumpInterval;
    }
}

it should work as :

counter++;
if (counter > 20){
     counter = 1;
}

But because it’s not, I have no idea sorry.