Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by DaveA · Jul 27, 2011 at 01:17 AM · arraycompile3.4strict

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)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaveA · Jul 27, 2011 at 03:59 AM 0
Share

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?

avatar image Eric5h5 · Jul 27, 2011 at 04:35 AM 0
Share

Right, although it would still be better to use Lists regardless.

avatar image
-1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Aug 17, 2011 at 05:56 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

(Yet another) Accessing array in another script - ref or copy? 1 Answer

C# Make array based on script variable 1 Answer

Manipulate arrays in Inspector with Editor Script? 2 Answers

Unity Freezes when accessing an Array in a different script GameObject 0 Answers

"The associated script cannot be loaded" on every Script in every GameObject in the project. Corrupted Project. 15 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges