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);
}

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.

1 Answer

1

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?

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