Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Draspur · Apr 02, 2012 at 08:52 PM · simplenotworking

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Draspur · Apr 25, 2012 at 09:41 PM 0
Share

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

avatar image
5

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Draspur · Apr 25, 2012 at 09:40 PM 1
Share

$$anonymous$$new it was something silly like that, thanks!

Sorry for late reply.

avatar image
4

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges