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
0
Question by riontamer · Dec 10, 2012 at 08:58 PM · javascriptarraysscore

Javascript / Unityscript adding elements of an array

A basic but vital question.

I have dice that spit out a number to an array that's a public var in a 'master' script.

var scoreArray = new Array();

if I do the general javascript means to add the items together in the array…

 for (var i=0; i<scoreArray.length; i++){ // for every item in the array
     total += scoreArray[i]; // stack-add them together in the variable total
 }    

I get an operator error in the console telling me that I can't mix the int variable total to the object elements of the array. I have read that Javascript / Unityscript arrays are objects, and I am using .push in the other dice scripts to append to the array and the unity reference manual says that these should, technically, be int additions rather than .add would be.

So what's the trick? I'm really not into the idea of recoding everything to C# at this point and I'd also like to know for future reference. Putting items in an array as objects sounds great in organizing GameObjects and the like, but if I'm just trying to tally a score, what should I do?

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Dec 10, 2012 at 10:26 PM

Don't use the JS Array class, use generic List.

 import System.Collections.Generic;
 var scoreArray : List.<int>;

Then:

 for (var i = 0; i < scoreArray.Count; i++){
     total += scoreArray[i];
 }
Comment
Add comment · Show 7 · 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 riontamer · Dec 10, 2012 at 10:40 PM 0
Share

thanks! I had read elsewhere that Javascript arrays are rather deprecated and pointless at this point. Is it better to orient oneself to using C# lists rather than Javascript lists, as well?

avatar image Eric5h5 · Dec 10, 2012 at 10:46 PM 0
Share

They aren't C# lists; a List is a List regardless of language. They are .NET Lists (or rather $$anonymous$$ono Lists, since scripting in Unity is built on $$anonymous$$ono). All collection types are the same in all languages, aside from the JS Array class which is specific to Unity and can only be used in Unityscript. http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F (Although there are more, such as Stack, Queue, and LinkedList; see the $$anonymous$$SDN docs.)

avatar image Statement · Dec 10, 2012 at 10:50 PM 0
Share

I've seen this kind of Array/builtinarray/List issue creep up from time to time. Perhaps there is some place in the docs or tutorials where we should tidy up to avoid $$anonymous$$ching people using Array? @Riontamer, it would be nice if you told us where/how you got into the habit of using Array so we can see if there are any improvements to make.

avatar image riontamer · Dec 10, 2012 at 10:55 PM 0
Share

For me, creating an array simply seemed appropriate considering the project I worked on before this was a Shuffle Bag script. The script involves generating two arrays, one of 0's and one of 1's, with a randomly moving cursor that 'picks' and 'removes' each item it lands on. When the bag is empty it refills with the same amount as configured before based on modifications. However, I will say that in looking for info on that subject, I found an article, like I had mentioned above, calling Array's 'deprecated and slow' but thought mostly of it being a performance thing than a getting-it-to-work thing.

avatar image Eric5h5 · Dec 10, 2012 at 10:58 PM 0
Share

It would be nice if the docs for the JS Array class mentioned Lists. In fact the docs could stand to stress .NET/$$anonymous$$ono a lot more than they do, since newcomers tend to be either unaware of it entirely or think of it as a "C# thing", which is too bad since that's obviously a huge amount of functionality they're missing.

Show more comments
avatar image
1

Answer by DeveshPandey · Dec 11, 2012 at 02:25 PM

Have you tried this?

 var total:Number = 0;
 for (var i=0; i<scoreArray.length; i++){ // for every item in the array
     total += Number(scoreArray[i]); // stack-add them together in the variable total
 }  
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

12 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 avatar image

Related Questions

Why can't I push and pop from string array? 1 Answer

What the heck is a 'function(System.Array): void' Type? 2 Answers

Highscoring and changing scripts (Java/UnityScript) 1 Answer

Referencing data in pre-defined array 1 Answer

JavaScript 3 Arrays questions 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