- Home /
How to declare typed array in UnityScript
I have a code like this.
public var tileButtonRects = new Array();
function ShowTileButtons(){
for(var i in [0,1,2]){
for(var j in [0,1,2]){
tileButtonRects[i+j*3] = new Rect(15+(i*28), 100+(j*26), 10, 15);
GUI.DrawTexture(tileButtonRects[i+j*3], tiles['1s'+ ((i+1)+(j*3)).ToString()]);
}
}
}
And I get this error.
Assets/GameMaster.js(31,38): BCW0028: WARNING: Implicit downcast from 'Object' to 'UnityEngine.Rect'.
As far as I understand the error message says that I need to declare the Object as Rect. But I couldn't find out how to do it.
I've read this document, and tried var tileButtonRects = Rect[];
, but it didn't work. Could you tell me how to declare a Array
of Rect
Object?
Edit
I get BCE0048: Type 'System.Type' does not support slicing.
error, with this code.
#pragma strict
var tileButtonRects = Rect[9];
function Start () {
}
function Update () {
}
If you're a beginner, learn to use List - there are 1000 examples on here.
I recommend generic lists over Unity's own List class. You'll have the same issue, otherwise.
Hoe, just to be clear you mean
using System.Collections.Generic;
then for example
private List<ParseUser> threeTags;
and then
threeTags = new List<ParseUser>();
and so on. (I didn't even realise unity had their own "List" class!) Exactly as Hoe ses don't use it, use the one in the example here!
Yes, I meant that kind of List. However, that's C# syntax. The OP is writing in UnityScript.
I'm a beginner, I'm glad to follow your instruction. But I'm using UnityScript(Javascript in Unity). Can I use C# List from UnityScript?
Answer by Hoeloe · Apr 22, 2014 at 07:38 AM
When defining a typed array (which you should ALWAYS use over the Array class), you need to define the length of the array. Arrays are fixed-length constructs. One they're initialised, you cannot change their length (if you need to, then you should be using generic Lists).
Instead of using var tileButtonRects = new Rect[];
, you need something along the lines of var tileButtonRects = new Rect[5];
, for example, which would create an array of length 5.
EDIT: Have updated my answer. It looks like you need the new keyword in this case, from a quick bit of research.
Thanks for your answer. I replaced as you wrote, then I got a error Assets/Game$$anonymous$$aster.js(4,23): BCE0048: Type 'System.Type' does not support slicing.
Any idea how to fix this?
Sounds like you're not setting up the array correctly. Post your full code.
it's possible you have ( ) rather than [ ]
But just forget it and change to List as in the examples. 1000s of examples here about using List
here's the awesome doco to book mark
http://msdn.microsoft.com/en-us/library/d9hw1as6(v=vs.90).aspx
List is not better than an array for fixed-length examples. Their performance is almost comparable, but they result in slightly messier code. Better to use an array if you can.
Oh, I've written Rect[9]
. I should have wrote new Rect[9]
. There is still IndexOutOfRange
error. But I think I can fix it myself. Thanks for your help!