Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by schaddem · Jan 26, 2013 at 07:41 AM · javascriptarraysclass

unityscript class + array problem

I want to create a class and then make an array of its objects.

If I do it like this:

 class strtest 
 {var a:int;}
 var  test:strtest[]; //and later: var test = new strtest[100];

Then I don't know how to access it's variables, since

 test[5].a=3;

gives "a is not a member of strtest" or so. Which as I understand it means that some pointer goes nowhere and a different syntax is required. I believe the variable should be accessible this way since I can modify it from the inspector.

Now if instead I use

 class strtest extends System.ValueType

I can access it as expected BUT it disappears in the inspector, which is annoying.

Solutions, anyone?

Side question: Am I supposed to be using classes at all when working with unityscript? (Or does that go against the concept of javascript)

Thanks in advance

Comment
Add comment · Show 2
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 Fattie · Jan 26, 2013 at 10:52 AM 0
Share

Don't use "arrays" - use LIst ..

note this question

http://answers.unity3d.com/questions/388075/remove-object-from-array-in-javascript.html

and many others

avatar image Loius · Jan 26, 2013 at 11:05 AM 1
Share

[]-arrays are fine, just don't use Array.

2 Replies

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

Answer by Loius · Jan 26, 2013 at 11:08 AM

When you

new MyType[x]

(unless you do create it as a public variable visible in the inspector), that only allocates space for X variables of type 'MyType'. You also have to create each one:

 var test : TestClass[] = new TestClass[100];
 
 for ( var ix : int = 0; ix < 100; ix++ ) {
   test[ix] = new TestClass();
 }

This is a thing I still get caught up on once or twice a week, after YEARS of working with it. >_<

If that's not your issue, try declaring "public var a" instead of just "var a". I'm pretty sure the default is public, but maybe there's a weird setup issue or maybe I have crazy memory.

If none of this is helpful, could you post the entire script, or make up an entire script that exhibits the problem?

Yes, you should definitely be using classes with Unityscript. I know that part of the answer's right. :)

Comment
Add comment · Show 4 · 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 Bunny83 · Jan 26, 2013 at 01:43 PM 0
Share

That's basically true, but when you declare a public array in the inspector, you don't actually have to initialize the array or the classes in the array if they are serialized. The Unity editor will create them automatically.

Of course when you do

 test = new strtest[100];

in Start or Awake it will overwrite the automatically generated Array + classes and you have to create the class instances manually like you said.

avatar image Bunny83 · Jan 26, 2013 at 01:46 PM 0
Share

Oh, btw the type of "test" should be TestClass[], not TestClass ;)

avatar image Loius · Jan 26, 2013 at 06:58 PM 0
Share

Augh, thanks. $$anonymous$$y compiler keeps telling me the same thing. xD

avatar image volkswagenb · Jun 18, 2013 at 05:00 PM 0
Share

Thank you Loius!!

avatar image
1
Wiki

Answer by schaddem · Jan 26, 2013 at 06:25 PM

Thank you your solution works.

So to make an array of classobjects you have to call each ones constructor indivdually after claiming its space.

 class testclass  //define class testclass  
 {var a:int;var b:int;}
 var  object:testclass[]; //announce array of objects
 var arraysize = 1000;
 
 Awake()
 {
   object=new testclass[arraysize]; //reserve space
   for (var i = arraysize; i--;) {object[i]=new testclass();} //call constructors (counting down was quicker in c++, it's probably the same in unityscript)
 }


As far as List() is concerned that's no replacement for built in arrays, it's a replacement for Array(). Built in arrays are fastest and not resizeable, List is somewhat slower(I've read about 50-60%, but not sure if thats correct) and resizeable, everything else should, according to public opinion, not be used (that includes java Array(), List is superior).

That said, List might save you running through empty entrys and might thus be faster.

@bunny: I don't understand your comment. If we dont call
object=new testclass[arraysize]; how is unity supposed to know the size of the built in array? Is there some way we can define an array of classobjects with less code and let unity do the work for us?

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 Loius · Jan 26, 2013 at 07:01 PM 0
Share

(that's not an answer; it should be posted as a comment)

If you have this:

class X extends $$anonymous$$onoBehaviour { var x : TestClass[]; }

You can change the size and values of x in the Inspector, and you don't have to initialize it in code (Unity's actually doing all the initialization and stuff for you as you resize it in the inspector)

avatar image schaddemm · Jan 26, 2013 at 08:09 PM 0
Share

Thanks, good to know.

Regarding speed comparision between List and .net Array, I've done some more research:

List/for: 1971ms (589725196)

Array/for: 1864ms (589725196)

List/foreach: 3054ms (589725196)

Array/foreach: 1860ms (589725196)

If this test is sound (it probably is) then we're talking about $$anonymous$$ARGINAL speed differences, something like 5%. In return we gain comfort and scaleability, and if we've got even a small amount empty listentries List() could easily be faster(if we run list vs list we could end up vs emptylistentries*emptylistentries = waste).

So now I believe that we should be using List for pretty much everything and inbuilt arrays only as a microoptimization for arrays with fixed size.

I'm a little bit worried about resource allocation at runtime though.

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

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error: "ArgumentException: get_deltaTime can only be called from the main thread" 1 Answer

How does UnityScript class scope work, when in same file? 0 Answers

Javascript / Unityscript adding elements of an array 2 Answers

JS to C# class conversion issue. 1 Answer

Creating a dynamic array of objects of custom class 1 Answer


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