- Home /
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!
Answer by aldonaletto · Apr 02, 2012 at 09:13 PM
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; } }
Also knew it was a clumsy script, all $$anonymous$$e are. Thanks for a better one!
Answer by BiG · Apr 02, 2012 at 09:04 PM
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 :)
$$anonymous$$new it was something silly like that, thanks!
Sorry for late reply.
Answer by rutter · Apr 02, 2012 at 09:09 PM
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.
Answer by garner · Apr 02, 2012 at 09:06 PM
it should work as :
counter++;
if (counter > 20){
counter = 1;
}
But because it's not, I have no idea sorry.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why my rect not working 1 Answer
FindGameObjectsWithTag not working 1 Answer
Quick Custom Inspector Question 1 Answer
need help with script 1 Answer