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 mononull · Feb 28, 2013 at 06:11 PM · javascriptarraynullreferenceexceptioninitialization

NullReferenceException with Javascript Array

I'm learning Javascript, normally use C#. I get a NullReferenceException with defining the names of my GUIText array in the Start Function. I'm not initializing the array right. Thanks for the help. And don't tell me to assign it in the inspector. I can get it to work that way, but I need to understand why I cannot initialize it in code.

 private var buttons:GUITexture[];
 private var names:GUIText[];
 
 function Start()
 {
     buttons = new GUITexture[3];
     names = new GUIText[3];
     names[0].text = "APlane";
     names[1].text = "BPlane";
     names[2].text = "CPlane";
 }
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

2 Replies

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

Answer by Eric5h5 · Feb 28, 2013 at 06:51 PM

You created the array, but you didn't initialize any of the entries.

 names[0] = new GameObject("", GUIText).guiText;
 names[0].text = "APlane";

This isn't anything to do with Javascript, by the way; the exact same thing applies to C#.

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
avatar image
1

Answer by Mikilo · Feb 28, 2013 at 06:47 PM

Hi!

I don't know the exact behavior in JS, but I assume it is same as C#.

When you allocate an array of element, you only allocate the array, not the element!

In your case, you need to do:

 this.names = new GUIText[3];
 this.names[0] = new GameObject("", typeof(GUIText)).guiText;
 this.names[1] = new GameObject("", typeof(GUIText)).guiText;
 this.names[2] = new GameObject("", typeof(GUIText)).guiText;
 // And do your stuff after.

Good luck.

Edit: With the corrected code.

Comment
Add comment · Show 9 · 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 · Feb 28, 2013 at 06:53 PM 0
Share

You can't create components with new.

avatar image mononull · Feb 28, 2013 at 07:01 PM 0
Share

Didn't work.

I don't understand what I'm doing different from the Unity example:

 // Exposes an float array in the inspector,
 // which you can edit there.
 var values : float[];
 
 function Start () {
     // iterate through the array
     for (var value in values) {
         print(value);
     }
 
     // Since we can't resize builtin arrays
     // we have to recreate the array to resize it
     values = new float[10];
 
     // assign the second element
     values[1] = 5.0;
 }
avatar image Mikilo · Feb 28, 2013 at 07:02 PM 0
Share

Eric is right. Thank to point that out.

Anyway, don't forget that new with [] will only allocate a container, not the content!

avatar image Eric5h5 · Feb 28, 2013 at 07:09 PM 0
Share

And in C# when you initialize names = new GUIText[3]; it will allocate memory for 3 new guiTexts so it isn't necessary to go through each element manually.

This is incorrect. You still need to actually create the GUIText objects. $$anonymous$$erely creating an array will not do this, in any language. See my answer.

I don't understand what I'm doing different from the Unity example

Primitive types (and structs) don't need to be created separately, since they have a default value. GUIText does not have a default value, other than null, so it must be created.

avatar image mononull · Feb 28, 2013 at 07:12 PM 0
Share

Why does this throw a NullReferenceException then?

 private var names:GUIText[];
 
 function Start()
 {
     names = new GUIText[3];
     
     names[0] = new GUIText();
     names[0].text = "alsdkf";
 }
Show more comments

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

11 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

Related Questions

Array values not being initialised unless the object with the (Player) script is selected in the inspector whilst the game is on 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

C# custom class array error in adding a new entry. 1 Answer

Pushing GameObject into JS Array returns NullReferenceException 1 Answer

NullReferenceException weird error 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