- Home /
Regarding Array and the new stricter compile in 3.4
Maybe it was bad practice? But it was convenient. I have a lot of this going on: I have a structure (class) I define. Then I make an Array of them, eg.:
class data
{
var count : int;
var name : string;
}
var listOfData = new Array()
....
listOfData[0] = new data();
or
var d = new data();
listOfData.Push (d);
....
listOfData[i].name = "Joe"; // for example
....
This worked fine, so I used it a lot. Now with the new strictness, I get errors on all such accesses, because it doesn't know what type the elements of that array are, right?
So what's the best way to fix this? Is there a way to declare or initialize the array variable? Or do I have to either create a new temp var of my type, assign, and use that? Or use '(listOfData[i] as data).name' everywhere?
(note this is a simplistic example, my actual code is much more involved and would be a bear to change, and global search/replace would be just as tedious, so I'm hoping for a declarative fix)
Answer by Eric5h5 · Jul 27, 2011 at 02:07 AM
The "new strictness" only applies to iOS/Android, not Mac/PC standalone/web, and it's not really new, it just applies to script compilation now instead of waiting to hit you with errors when you try to make a build. (And this is in fact what the old Unity iPhone used to do when it was a separate app, so it's definitely not new.)
Anyway, yes, it's kind of bad practice to use the JS Array class; use generic Lists instead. Better/faster/easier (you don't have to manually cast stuff to work with #pragma strict). You're going to have to do a lot of manual casting anyway if you stick with Array, so it would be a good idea to take the opportunity to switch to Lists.
Thanks, I'll look into Lists then. So I'm getting this because my build platform is set to Android, right? If I'd built for PC/$$anonymous$$ac it wouldn't complain?
Right, although it would still be better to use Lists regardless.
Answer by taio84 · Aug 17, 2011 at 05:39 PM
I think the only problem you are having is that you have to declare the type of the variables, so in your case simply have :
var listOfData : Array = new Array();
var d : data = new data();
I'm still doing this in unity 3.4
That's not what the issue is. var listOfData = new Array();
will type "listOfData" as Array. You don't have to write out "var listOfData : Array" if you don't want to (whether you should is another discussion that I won't get into). This is called type inference, where the type is inferred by the value you supply, and has nothing to do with dynamic typing or Unity 3.4.
Answer by Peter G · Jul 27, 2011 at 02:07 AM
Unfortunately I believe a thorough search and replace is needed. That's dynamic typing so you really should stay away from it anyway, and I can't think of any hacks that would do what you want. I would suggest switching to a Generic list if you are going to have to rewrite all your arrays. It's type safe.
Now that I think of it, you could do something like this for a universal search and replace.
Your answer
![](https://koobas.hobune.stream/wayback/20220612155923im_/https://answers.unity.com/themes/thub/images/avi.jpg)