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
1
Question by dansav · Aug 07, 2011 at 06:21 AM · javascriptarrayvariablefunctionpass

passing a javascript array into a function sorts it?

I have no idea what's going on here, but when I pass a javascript array into a function it sorts it??? I don't want it sorted. What's causing the output to be sorted?

function linreg(xarray,yarray) {
Debug.Log(xarray);
Debug.Log(yarray);
}

x=new Array(1,2,3,4,5,6,7,8,9,10);
y=new Array(10,9,8,7,6,5,4,3,2,1);

output =
1,2,3,4,5,6,7,8,9,10
1,2,3,4,5,6,7,8,9,10

Thanks,
Dan

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Aug 07, 2011 at 06:36 AM

That's not what you actually get for output. Instead, it prints the values you have listed there when declaring the arrays. If you notice, your output has "11" there, which isn't part of the array. I guess you have some more code that's printing those values, because arrays aren't sorted when they're passed into a function.

As an aside, always type variables when creating functions. Also, the convention is to use uppercase for function names and lowercase for variable names, which makes code easier to understand.

 function Linreg (xarray : Array, yarray : Array) {

Also, there's rarely any reason to use the JS Array class. It's slow and not type-safe. Use built-in arrays for best speed, or if you need dynamically-sized arrays, use generic List.

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 dansav · Aug 07, 2011 at 07:21 AM 0
Share

The 11 was a typo which I fixed. Thanks for the general tips and speed suggestions. I checked the script independently of the main script it is in and it works. If I change the variable names it works. The main script has many statistics javascript functions I have been porting from html/javascript to unity. $$anonymous$$any of the variables in these other functions have var preceding them. Is there a difference in a var declaration inside a function from javascript/html to unityscript that could be causing problems?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Issues referencing variables between functions 1 Answer

Different way to access class variables? 1 Answer

functions file and NullReferenceException: Object reference not set to an instance of an object 1 Answer

Getting a variable from a GameObject inside a 2d array? 1 Answer

Add a temporary variable to an array 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