Search Unity

Type 'Object' does not support slicing (error)

Discussion in 'Scripting' started by DaveyJJ, Jan 24, 2009.

  1. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    OK, I am doing something wrong trying to get a selected item from an array into a variable.

    Code (csharp):
    1.  
    2. function SelectWord ()
    3. {  
    4.     // --- create basic word list in an array of words
    5.    
    6.     wordArray = new Array (
    7.         ["true","word1","definition1"],
    8.         ["true","word2","definition2"],
    9.         ["true","word3","definition3"],
    10.         ["true","word4","definition4"],
    11.         ["true","word5","definition5"],
    12.         ["false","word6","thisisnotarealword6"],
    13.         ["false","word7","thisisnotarealword7"],
    14.         ["false","word8","thisisnotarealword8"] // no comma at end
    15.     );
    16.    
    17.     // --- end basic word list
    18.    
    19.     // check PlayerPrefs recent words list up to 10 times
    20.     // to see if word chosen was used within the last 100 plays
    21.     var loopCount = 0;
    22.     var loopLimit = 10;
    23.     var keepLooping = true;
    24.     while( keepLooping ) {
    25.         // choose random word for gameplay based on wordArray.length
    26.         wordIndex = Random.Range(0,wordArray.length);  
    27.                              
    28.         if( !IndexTooRecent( wordIndex ) || loopLimit == loopCount++) {
    29.             keepLooping = false;
    30.         }        
    31.     }
    32.    
    33.     // add boolean, word and definition into static variables to be used in the various gameplay scenes
    34.     isItAWord = wordArray[ wordIndex ][0];
    35.     wordShown = wordArray[ wordIndex ][1];
    36.     wordDefinition = wordArray[ wordIndex ][2];
    37.  
    38. }
    39.  
    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!
     
  2. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    wassup dave? hope you've been well monkey!

    Code (csharp):
    1. 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 :)

    <erec... hehehe!>
     
  3. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    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:

    Code (csharp):
    1. wordArray = new Array(
    2.     ["true","word1","definition1"],
    3.     ["true","word2","definition2"]
    4. );
    5.  
    6. // Instead of this:
    7.  
    8. isItAWord = wordArray[ wordIndex ][0];
    9.  
    10. // ...do this:
    11.  
    12. var innerArray : Array = wordArray[ wordIndex ];
    13. 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:

    Code (csharp):
    1. wordArray = [
    2.     ["true","word1","definition1"],
    3.     ["true","word2","definition2"]
    4. ];
    5.  
    6. // Then this is fine:
    7.  
    8. 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.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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. ;)

    --Erec
     
  5. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    Thank you Neil and Cire!

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

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    Thanks Neil,

    I used your second suggestion and it works perfectly.
     
  7. unitydevist

    unitydevist

    Joined:
    Feb 3, 2009
    Posts:
    45
    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.