x


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!

more ▼

asked Apr 02 '12 at 08:52 PM

Draspur gravatar image

Draspur
32 2 2

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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;
    }
}
more ▼

answered Apr 02 '12 at 09:13 PM

aldonaletto gravatar image

aldonaletto
42.5k 16 43 202

Also knew it was a clumsy script, all mine are. Thanks for a better one!

Apr 25 '12 at 09:41 PM Draspur
(comments are locked)
10|3000 characters needed characters left

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 :)

more ▼

answered Apr 02 '12 at 09:04 PM

BiG gravatar image

BiG
4.8k 4 14 51

Knew it was something silly like that, thanks!

Sorry for late reply.

Apr 25 '12 at 09:40 PM Draspur
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 02 '12 at 09:09 PM

rutter gravatar image

rutter
5.4k 2 11

(comments are locked)
10|3000 characters needed characters left

it should work as :

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

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

more ▼

answered Apr 02 '12 at 09:06 PM

garner gravatar image

garner
120 1

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2059
x563
x104
x75
x34

asked: Apr 02 '12 at 08:52 PM

Seen: 471 times

Last Updated: Apr 25 '12 at 09:41 PM