Type 'Object' does not support slicing (error)

OK, I am doing something wrong trying to get a selected item from an array into a variable.

function SelectWord ()
{	
	// --- create basic word list in an array of words
	
	wordArray = new Array (
		["true","word1","definition1"],
		["true","word2","definition2"],
		["true","word3","definition3"],
		["true","word4","definition4"],
		["true","word5","definition5"],
		["false","word6","thisisnotarealword6"],
		["false","word7","thisisnotarealword7"],
		["false","word8","thisisnotarealword8"] // no comma at end
	);
	
	// --- end basic word list
	
	// check PlayerPrefs recent words list up to 10 times
    // to see if word chosen was used within the last 100 plays
    var loopCount = 0;
    var loopLimit = 10;
    var keepLooping = true;
    while( keepLooping ) {
        // choose random word for gameplay based on wordArray.length
        wordIndex = Random.Range(0,wordArray.length);   
                             
        if( !IndexTooRecent( wordIndex ) || loopLimit == loopCount++) {
            keepLooping = false;
        }        
    }
   
    // add boolean, word and definition into static variables to be used in the various gameplay scenes
    isItAWord = wordArray[ wordIndex ][0];
    wordShown = wordArray[ wordIndex ][1];
    wordDefinition = wordArray[ wordIndex ][2];

}

The error, “Type ‘Object’ does not support slicing”, is being called on this line …

isItAWord = wordArray[ wordIndex ][0];

But I assume it would also apply to the following two as well.

The goal is to randomly select from my array of words (testing to see if it was a recently played word) and then push the result (true/false, word, definition) from the array into the three variables. I think it has something to do with how I am trying to grab the one item (i.e., see here … http://boo.codehaus.org/Slicing) but don’t know how to resolve it.

Any help appreciated.

Also, what would I do to put the wordArray code into a completely separate script … in other words, to remove it from this one to allow someone else to compile it and then simply call it into that line like an include?

Again, thanks!

wassup dave? hope you’ve been well monkey!

isItAWord = wordArray[ wordIndex ][0];

this is something i haven’t had time to suss out but it looks to me like this is a dimensional array in js. as i understand it you’d need to use c# for this context but i don’t have a good handle on it. take it with a grain of salt. erec, maybe you can shed some light? i could use it for an upcoming project if u got time :slight_smile:

<erec… hehehe!>

The error message means “you’re trying to use this thing as if it were an array, but I’m not sure that’s possible because you haven’t told me what type of object it is”. You apparently either have #pragma script in effect or you’re developing for the iPhone, since your code would normally work fine.

You’re using Array at the top of the function. When you use this unqualified name in Javascript, you get UnityEngine.Array, which thinks of everything you put into it as a System.Object (an object of unknown type). You’re putting arrays into it, but when you pull them out later, it has no idea what they are any more, and you have to tell it:

wordArray = new Array(
	["true","word1","definition1"],
	["true","word2","definition2"]
);

// Instead of this:

isItAWord = wordArray[ wordIndex ][0];

// ...do this:

var innerArray : Array = wordArray[ wordIndex ];
isItAWord = innerArray[0];

Another solution, which is probably better if you don’t need the dynamic resizing features of UnityEngine.Array, would be to use the array literal syntax [a, b, c] for the outer array as well as the inner arrays:

wordArray = [
	["true","word1","definition1"],
	["true","word2","definition2"]
];

// Then this is fine:

isItAWord = wordArray[ wordIndex ][0];

When you use the array literal syntax you get a System.Array instead of a UnityEngine.Array. This is what the docs are referring to when they say ‘built-in array’. Providing you make sure that all the objects you put into the array are of the same type, the array literal will automatically become a statically typed array of that type and you won’t have to tell the compiler what it is each time you access it.

On the other hand, if you put different kinds of object into an array literal, it will become an array of System.Object and you’ll be back to the behaviour you get from UnityEngine.Array, so that would probably be unhelpful.

Nope, the C# thing is “foo[1,2]” (or at least if you want to declare such an array; using it in JS is OK). “foo[1][2]” is something different. :wink:

–Erec

Thank you Neil and Cire!

that really cleared it up for me. hope it helped you too D!

Thanks Neil,

I used your second suggestion and it works perfectly.

I got inventory working on iPhone thanks to you!

I’m going to release a mega-bundle of all the gameplay elements from all the tutorials with switchable camera control styles working on iPhone and mouse/keyboard.