Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by Wolfos · Jun 02, 2011 at 04:42 PM · javascriptoperator

+= operator doesn't work (Javascript)

Newest code, speed just remains 0. When I add var to the second one, speed doesn't remain 0.

 var speed = 0;
 var accel = 10;
 var x = 0;
 var y = 0;
 var z = 0;
 
 function Update () {
     speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * accel;
     z = z + speed;
     print(speed);
     transform.Translate(x, 0, z);
 }


EDIT: Never mind, it works now. What the problem was, is that "accel" is just too small to actually make a difference, Unity prints this as 0.

Comment
Add comment · Show 4
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 flaviusxvii · Jun 02, 2011 at 04:52 PM 0
Share

Post the code where you're using it.

avatar image superventure · Jun 02, 2011 at 04:54 PM 0
Share

Post more code so we can see what your doing exactly

avatar image SirGive · Jun 02, 2011 at 05:35 PM 0
Share

You're re-declaring your variables!

avatar image Wolfos · Jun 05, 2011 at 02:26 PM 0
Share

Actually, I did keep the declarations on top. I'm really not a program$$anonymous$$g noob but it just doesn't work. I have programmed Actionscript 3, which is very similar, so I kind of know how declaring variables work.

The point is, that when I don't use var (only for the declaration), nothing happens.

4 Replies

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

Answer by MacMac098 · Jun 05, 2011 at 02:31 PM

var speed : float = 10;

function Update () {

 var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
 var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
 transform.Translate(x, 0, z);

}

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
3

Answer by Graham-Dunnett · Jun 02, 2011 at 05:00 PM

 var speed = 0;
 var accel = 10;
 var x = 0;
 var y = 0;
 var z = 0;
 
 function Update () {
     speed = speed + Input.GetAxis("Vertical") * Time.deltaTime * accel;
     z += speed;
 
     transform.Translate(x, 0, z);
 }

Note that you use var to tell the compiler about your variables. Once you have declared them don't use the var keyword when you use the variable.

Comment
Add comment · Show 4 · 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 Wolfos · Jun 02, 2011 at 05:09 PM 0
Share

Yep, that's what I did. It solves the problem but it's still silly and I'm used to using +=. removing the var completely stops it from working somehow.

avatar image almo · Jun 02, 2011 at 05:10 PM 0
Share

$$anonymous$$eep the var in the declaration up top. Leave it out later when you use the variable.

avatar image equalsequals · Jun 02, 2011 at 05:33 PM 0
Share

The reason why removing var works is because by using var you are actually creating a new variable local to your method, when you obviously want to assign it to your class variable of the same identifier. This becomes a problem of scope.

avatar image Ejlersen · Jun 02, 2011 at 05:33 PM 1
Share

Doesn't really make any sense to have "var z += speed;", because what should it add speed to? Zero? Infinity?

avatar image
0

Answer by Wolfos · Jun 05, 2011 at 02:42 PM

Thanks Mac! What is the standard datatype it uses? Integer? Coming from Actionscript, I assumed Javascript was kind of the same in managing variables as Flash. It's not. I'll remember that it's not Actionscript and I'll follow some Javascript tutorials.

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 aldonaletto · Jun 05, 2011 at 03:21 PM 0
Share

It inferes the type from the initialization value, if any (or from other expressions in the context). If you had written:
var speed = 0.0;
the compiler would assume speed as a float.
Personally, I don't believe in type inference, so I always declare explicity all variable types (tempting $$anonymous$$urphy's Laws is not a good practice...)

avatar image
0

Answer by MacMac098 · Jun 05, 2011 at 02:45 PM

var Speed : float = 0;

var accel : float= 10;

private var RelativeSpeed : float = 0;

private var x = 0;

private var z = 0;

function Update () {

 Speed += Input.GetAxis("Vertical") *  Time.deltaTime * accel;
 
 z = z + Speed;
 Speed -= RelativeSpeed;
 print(Speed);
 transform.Translate(x, 0, z);

}

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

11 People are following this question.

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

Related Questions

EXTREMELY basic scripting question. 2 Answers

Ternary operator fails, but verbose comparison doesnt? 1 Answer

How do I compare two binary values in Javascript? 1 Answer

Color index operator not working properly 1 Answer

Having random operators in an equation? 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