# Type Object does not support slicing Unity3D

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 () {
}


The error states as follows:

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

• Please comment #pragma strict Hope this helps – user35491 Sep 26 '13 at 12:27

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

• 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). – doppelgreener Jun 26 '12 at 21:34

Javascript doesn't support multidimensional arrays well, so either switch to storing an array of objects or switch to C# in order to use multidimensional arrays.

This advice came up in answers to this Unity question. Meanwhile, according to this other 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[,];


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.

• +1 for a good answer that was probably used without acceptance. – MichaelHouse Jun 14 '12 at 14:02
• well this answer omits a couple crucial facts (which I'm editing in) so vish may not have gotten this working – jhocking Sep 26 '13 at 12:52
• @jhocking What were the omitted crucial facts? If it's the poor native support for multidimensional arrays, that's true - just not in Unity's version of it, which supports multidimensional arrays just fine. They're used right there in the post - that's native JavaScript code in Unity, if I understand correctly. – doppelgreener Sep 26 '13 at 13:09
• oh I didn't notice the dates on the responses on those links; apparently this used to be an issue (as in a couple years ago) but it's since changed. I'm not going to edit the post back because I seem to be a little mistaken here, but maybe add a note that the information in the first link is dated so that can cause confusion – jhocking Sep 26 '13 at 16:30