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 mthicke2 · Apr 15, 2013 at 01:45 AM · arrayobjectclass

array of objects

I want to save a string (word) and int (score) into an array and then sort based on the int.

Im using Javascript. I've tried making a class then saving an object in the array:

 public class wordClass 
 {
     var word : String;
     var score : int;
 }
 
 var wordObject = wordClass();
 
 wordObject.word = wordString;
 wordObject.score = wordScore;
 
 PermanentVariables.wordsFound.push(wordObject); 
 Debug.Log("CLASS TEST: " + PermanentVariables.wordsFound[1].word);
 
 
 //PermanentVariables are is where the array is stored. I made this so that I can access these from any scene
 public static class PermanentVariables
 {
     public var wordsFound = new Array();
     public var gameScore : int;
     public var levelScore : int;
 
 }

I get the error: Assets/Scripts/GameScript.js(112,77): BCE0019: 'word' is not a member of 'Object'.

Is there a better way to do this? I haven't written the sort code yet.

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 Chronos-L · Apr 15, 2013 at 01:56 AM

Please read this. Javascript array is untyped, so if you want type-specific operation, you need to do type-casting:

 Debug.Log("CLASS TEST: " + (PermanentVariables.wordsFound[1] as wordClass).word);



Why don't you use the .NET's List< T > instead; the only thing Javascript array can do but the List< T > can't is to store different types of variable, which is not needed at all in your case.

Comment
Add comment · Show 9 · 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 mthicke2 · Apr 15, 2013 at 02:00 AM 0
Share

If I declare the variables in the wordClass class without specifying a type I get the same error. I imagine I'd get the same error no matter what array type I use? maybe not?

avatar image jiteshvm · Apr 15, 2013 at 02:04 AM 1
Share

you can use a Dictionary too

 // declaration
 var myDict = new Dictionary.<int,String>();
 
 // insert a value for the given key
 myDict[any$$anonymous$$ey] = newValue;                 
 
 // retrieve a value
 var thisValue = myDict[any$$anonymous$$ey];
avatar image Chronos-L · Apr 15, 2013 at 02:14 AM 0
Share

You can put a #pragma strict in the first line of your script, it will make you write you script in a stricter/explicit syntax.

@jitesh's solution is good as well; From your code, I think that you are making a scramble/word-guessing game, using a Dictionary is a viable alternative. However, I would suggest you to use Dictionary. < string, int >() ins$$anonymous$$d.

avatar image Chronos-L · Apr 15, 2013 at 03:48 AM 1
Share

Use Dictionary< string, int >() ins$$anonymous$$d, the string will become the key, i don't think you will have 2 different score for the same string right? So use string for the key, and int as the value/score.

avatar image Eric5h5 · Apr 15, 2013 at 04:06 AM 1
Share

the only thing Javascript array can do but the List can't is to store different types of variable

Actually you can store different types of variables in a List by using List.< Object >, though you then have the same casting issues that you have with Array. Although the List is still faster. And yes that makes JS Array completely useless. ;)

Show more comments

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

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

Related Questions

adding classes to arrays 2 Answers

What are GameTiles? 1 Answer

Instantiating objects from a class? (C#) 1 Answer

Cannot edit values of Class when created using .JSON file 1 Answer

Is there any option to put objects together? 0 Answers


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