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 SmooveB · Dec 03, 2011 at 11:19 PM · classbuiltin array

built in array in class

I must be doing something wrong as far as using built in arrays in classes. Here is my test code:

 class DemoData
 
 {
 
     var x : float;
 
     var y : float;
 
     var z : float;
 
     var name : String;
 
 }
 
  public var _iUsersTest:DemoData[] = new DemoData[3]; //this works!
         
  class UserData
 
  {
       
        public var _iUser : DemoData = new DemoData(); // this works!
 
        public var _iUsers : DemoData[] = new DemoData[3]; // this does not!
     
        function UserData() { }
 
 }

 private var myData : UserData;
 
 function Awake () 
 {
 
      myData=new UserData();

      myData.iUsers[0].name = "fred"; //throws NullReferenceException error
 
 }



So _iUsers throws and NullReferenceExeption any time I try to set any of its elements, but the same syntax outside a class works just fine. How do I make _iUsers work?

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

1 Reply

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

Answer by syclamoth · Dec 03, 2011 at 11:32 PM

The problem here is that while the array has been initialised with a length, none of its members have! So, at this point it's a 3-long array filled with null objects. Now, there are a few ways to get around this- one way is to make DemoData a struct, instead of a class- structs can never be null, they simply automatically initialise with default values. They are also treated as value objects, not reference objects.

Another way would be to automatically fill _iUsers in the constructor for UserData-

 function UserData() {
     for(var i : int; i < _iUsers.Length; i++)
     {
         _iUsers[i] = new DemoData();
     }
 }

This guarantees that there will be objects in every slot of the array at the start.

I can understand where the confusion would arise- if you put an array in the base class (technically deriving from MonoBehaviour, but you can't see that because grr javascript grr), Unity automatically serializes and fills it so that it can be seen in the inspector. However, this is not functionality provided by the array itself, rather magic contained in the MonoBehaviour base class. If myData were public, the magic would extend to it as well- but since it's private, the Inspector can't see it, and doesn't serialize it.

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 SmooveB · Dec 03, 2011 at 11:58 PM 0
Share

Thanks for the answer and thanks even more for the great explanation. Probably saved me from drawling the wrong conclusion from all of this. Did not consider the now obvious fact that things in the inspector get initialized and set.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

Reflection on Unity class 1 Answer

upgrading charachter like pou game 1 Answer

unity C# - Protected Class That Should Be Accessable 1 Answer

add class or script to child gameObjects 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