Tell me more ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am getting the following error in my code and I can't seem to understand why. Can anyone help me with it?

This is my current code. The line causing the error is marked in a comment near the end.

var rows : int = 4;
var cols : int = 4;
var totalCards : int = cols * rows;
var matchesNeedToWin : int = totalCards * 0.5; 
var matchesMade : int = 0;

var cardW : int = 100;
var cardH : int = 100;

var aCards : Array; 
var aGrid : Array;

// This Array will store the two cards that the player flipped
var aCardsFlipped : ArrayList; 

// To prevent player from clicking buttons when we don't want him to
var playerCanClick : boolean; 

var playerHasWon : boolean = false;     


class Card extends System.Object {
var isFaceUp : boolean = false;
var isMatched : boolean = false;
var img : String;

    function Card () {
        img = "robot";
    }
}

function Start () {

    var i : int = 0;
    var j : int = 0;

    playerCanClick = true;
    aCards = new Array ();
    aGrid = new Array ();
    aCardsFlipped = new ArrayList ();

    for ( i = 0; i < rows; i++) {
        aGrid [i] = new Array ();

            for (j = 0; j < cols; cols++ ) {
            aGrid [i] [j] = new Card (); // <------ Error over here
        }
    }
}


function Update () {
    Debug.Log("Game Screen has loaded");
}

The error states as follows:

Error BCE0048: Type 'Object' does not support slicing. (BCE0048) (Assembly-UnityScript)

share|improve this question

2 Answers

Use Unity's multidimensional arrays rather than jagged arrays, in which this appears to be an unsolved problem. According to this Unity question, you can declare them like this:

var a : float[,];

Or in your case, I'm guessing the syntax would be this (with or without the new, I'm not sure):

aGrid = new Card[,];

So instead of your problematic line you'd have this:

aGrid[i,j] = new Card();

If you really do want to use a jagged array you need to use a workaround (according to that first link): use the Array.Add() method rather than directly setting the variable at each index.

share|improve this answer
+1 for a good answer that was probably used without acceptance. – Byte56 Jun 14 '12 at 14:02

Like Jonathan Hobbs said, I've made a few changes.
This is what I modified those code

var aCards:Array
var aGrid:Card[,]; //<-- change how you declare this variable to multidimensional arrays
var aCardsFlipped:ArrayList;

And change this in the Start function

function Start () {
    playerCanClick = true; 
    aCards = new Array();
    aGrid = new Card[rows,cols]; //<-- I instantiate it as rows*cols array
    aCardsFlipped = new ArrayList();
    for (var i=0;i<rows;i++)
    {
        for (var j=0;j<cols;j++)
        {
            aGrid[i,j] = new Card(); //<-- Then assign the value like this
        }
    }
}

Hope this help

share|improve this answer
I would upvote your answer if you actually made it clear what you changed. Others with the same problem (but different code) will come to this site for a solution, so a good answer would let them know what's going on, too, so they can fix it for themselves (since copying+pasting the code above would do nothing for them, and it contains no value to their situation unless they dive in and try to find out what you changed). – Jonathan Hobbs Jun 26 '12 at 21:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.