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 /
avatar image
0
Question by Clonkex · Oct 27, 2014 at 11:08 AM · javascriptarrayclassbuiltin array

Why Does Initialising Built-In Array of Classes Require Constructor?

Say I have the following short script:

 #pragma strict
 
 class myArrayInfo
 {
     var name:String;
 }
 
 static var myArray:myArrayInfo[];
 
 function Start()
 {
     myArray=new myArrayInfo[3];
     myArray[0].name="Random name 1";
     myArray[1].name="The answer to the ultimate question of life, the universe, and everything is...";
     myArray[2].name="...42.";
 }

I would expect that to work, but I get NullReferenceException: Object reference not set to an instance of an object at line 13, where I attempt to set index 0 of the array.

So I tried doing this:

 #pragma strict
 
 class myArrayInfo
 {
     var name:String;
     function myArrayInfo(){}
 }
 
 static var myArray:myArrayInfo[];
 
 function Start()
 {
     myArray=new myArrayInfo[3];
     myArray[0]=new myArrayInfo();
     myArray[1]=new myArrayInfo();
     myArray[2]=new myArrayInfo();
     myArray[0].name="Random name 1";
     myArray[1].name="The answer to the ultimate question of life, the universe, and everything is...";
     myArray[2].name="...42.";
 }

And this works. But why?! Why is it necessary to make it run an empty function (the constructor) to be assigned a value? The constructor clearly isn't doing anything, unless I'm completely misunderstanding how constructors work.

Comment
Add comment · Show 4
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 · Oct 27, 2014 at 10:40 PM 0
Share

Why is it necessary to make it run an empty function (the constructor)

It's not; get rid of that. It doesn't serve any purpose. You only need a constructor if you want to assign values when creating an instance.

avatar image Clonkex · Oct 27, 2014 at 11:48 PM 0
Share

@Eric5h5 Then how do I make the code work? As I said, without using an empty constructor on each element it gets a NullReferenceException.

avatar image Eric5h5 · Oct 27, 2014 at 11:52 PM 0
Share

Just initialize the variables in the array, since they are null until initialized.

 var myArray = new $$anonymous$$yClass[5];
 for (item in myArray) item = new $$anonymous$$yClass();

An empty constructor doesn't do anything, so it should be removed.

avatar image Clonkex · Oct 28, 2014 at 12:01 AM 0
Share

@Eric5h5 :O WOW, ok. I didn't know you could do that! I never knew you could do new $$anonymous$$yClass() without actually defining a constructor! Learn something new every day...

1 Reply

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

Answer by steakpinball · Oct 27, 2014 at 01:41 PM

The constructor primarily gets a memory location for a new object. It optionally also initializes the object.

When creating an array of objects the array is initialized with all null references ( 0x0000000 ). You need to call the class's constructor to get a valid memory location and therefore an object. Primitive types work a little differently in that you don't need to call a constructor for it to be in memory since they are value types.

An example: If you had

 var info : myArrayInfo;
 info.name = "Name";

it would return an error since the constructor has not been called.

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 Clonkex · Oct 27, 2014 at 10:26 PM 0
Share

Huh, I never realised the constructor did more than just initialise the member variables. I call myself a good programmer and then I have to ask questions like these. *facepalm* (argh I hate Unity Answers' website! I can't even escape the asterisks like on SE sites!)

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

29 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 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

Reading and Storing External Data into Memory (From Text) 1 Answer

Creating a dynamic array of objects of custom class 1 Answer

Different way to access class variables? 1 Answer

Static array with custom class? 1 Answer

How to access a variable within a multidimensional array of class 2 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