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 supra411 · Nov 09, 2013 at 01:04 AM · arrayclasses

Trying to create an class array with two properties

Hi:

I'm trying to create a javascript (not builtin) array that has two properties. The variable arrays are called p1 and p2. So I'm trying to add values to the properties in the array. However, when I'm adding values (num and suit) to the array (using push) I get the following error message:

MissingMethodException: card[].Push

Here's the following code. I'm sorry if I'm not being clear. If you have any questions, please ask. I'm trying to get this simple code to execute first before school tomorrow, so I'll be checking this page often.

Thanks!

 //create number and suit classes for p1 and p2.
 class card {
     var num;
     var suit;
     function card (n,s) {
         num = n;
         suit = s;
     }
 }
 
 var p1 : card[];
 var p2 : card[];
 
 //nu is number and su is suit.
 
 var nu;
 var su;
 
 function shuffle (nu,su) {
     p1.Push(nu,su);
 }
 
 function Start () {
     Debug.Log ("shuffle start");
     shuffle(1,"S");
     Debug.Log ("shuffle end");
     Debug.Log (p1.suit);
 }
Comment
Add comment · Show 3
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 Eric5h5 · Nov 09, 2013 at 01:20 AM 0
Share

You need to define all variable types. "var num" isn't right; you need to use "var num : int" or whatever the type is, same in the function declaration. The convention is that classes are upper-case, variables are lower case. Don't use variable names like "nu"...just write them out; you don't gain anything by having obscure variable names except difficult-to-read code.

avatar image Huacanacha · Nov 09, 2013 at 01:37 AM 0
Share
  1. You aren't creating any arrays, you're just creating variable to hold arrays

  2. When you Push you are trying to add two variables, not a single 'card'. $$anonymous$$aybe you mean to create a new card with 'nu' and 'su' and add that?

  3. Variables need types (as mentioned by Eric5h5).

  4. If you use arrays you need to specify their size on creation. If you don't know their size when you create the array you probably want a List (as described by rutter)

  5. You can't reference an array as if it was an instance of the type it stores, so p1.suit will fail. You need to extract the object then use it... to get the first item use p1[0].

I'm sure there are more issues with your code but that's what I picked up with a quick parse. The overriding impression I get is: You need to spend some time learning to code properly first. It will be worth it in the long run (and even in the short term).

avatar image supra411 · Nov 09, 2013 at 02:22 AM 0
Share

Thanks! I've defined the variables in the program.

I'm new to coding, and that is all I have in regards of coding because I want to solve this issue first.

I basically want to create two (p1 and p2) list variables (because their sizes will change) that will hold 2 properties (the number of the card and the suit of the card). I also need the 2 properties tied together, since they're suppose to be one card. The lists will grow as the cards are being "shuffled" and "given" to the player

So my questions are: 1. I read the wiki page that rutter sent to me, and I think I want the ArrayList or the 2D array. Which one would work, or does neither works for these purposes? 2. How can I add an item (with the two variables) to that list or 2D array?

Sorry for my lack of understanding and n00b-ness. I'm a type of "learn as I do" person, so please (pretty please) help me out!

Please see below for the updated code:

Thanks!

 class card {
     var num : int;
     var suit : String;
     function card (n : int, s : String) {
         num = n;
         suit = s;
     }
 }
 
 var p1 : card[];
 var p2 : card[];
 
 function shuffle (n : int, s : String) {
     p1.card (n, s);
 }
 
 function Start () {
     Debug.Log ("shuffle start");
     shuffle (1,"S");
     Debug.Log ("shuffle end");
     var firstCard : p1[0];
     var firstSuit : String = firstCard.suit;
     Debug.Log (firstSuit);
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by rutter · Nov 09, 2013 at 01:07 AM

A card[] has fixed length. If you want a structure with dynamic length, you might instead use a List.

Handy reading on the Unity wiki, including sample code: Which Kind Of Array Or Collection Should I Use?

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 supra411 · Nov 09, 2013 at 01:15 AM 0
Share

Hi:

Thanks for the recommendation! With an ArrayList; how will I declare the ArrayList variable as a class?

Thanks!

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

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

Related Questions

"NullReferenceExecption: Object reference not set to an instance of an object" error when using array of own made class objects. 1 Answer

Class Array referencing 1 Answer

How to access individual class elements, that has an array, that is in a list for c# 1 Answer

adding classes to arrays 2 Answers

Trying to create an array within an array (Round 2) 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