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 DiGiaCom-Tech · Jun 28, 2015 at 01:31 AM · inspectorarraysclasses

Problems With Variable Class Arrays...

I've tried researching this and am just not finding how to set this properly. What I'm trying to do is load inspector variable class arrays within a mono behaviour class attached to a game object when the game starts.

Here is what I have so far ... two variable class arrays; one nested within the other. On the inspector I can resize & edit both variable class arrays as needed. However, at run-time the start event fails with the following error message because everything is NULL (or at least the strings are):

NullReferenceException: Object reference not set to an instance of an object ClassSubClass.Start () (at Assets/_Main/Scripts/ClassSubClass.cs:36)

Here is my class code ... what am I doing wrong and how can I fix this?

 using UnityEngine;
 using System.Collections;
 
 public class ClassSubClass : MonoBehaviour
 {

     // Sub Class Details
     [System.Serializable]
     public class SubClass
     {
         public string SubName;
         public string SubText;
         public int SubWeight;
         public float SubMin;
         public float SubMax;
     }

     // Main Class Details
     [System.Serializable]
     public class MainClass 
     {
         public string MainName;
         public string MainText;
         public float MainMin;
         public float MainMax;
         public SubClass[] SubList;
     }
     public MainClass[] MainList;

     // Initialization of class arrays
     void Start()
     {
         // Load Classes
         MainList = new MainClass[1];
         // Populate the List
         MainList[0].MainName = "First Item (0)";
         MainList[0].MainText = "Hello World";
         MainList[0].MainMin = 10f;
         MainList[0].MainMax = 20f;
         // Now the sublist
         MainList[0].SubList = new SubClass[1];
         MainList[0].SubList[0].SubName = "First Sub Item (0.0)";
         MainList[0].SubList[0].SubText = "Hello Sub World";
         MainList[0].SubList[0].SubWeight = 30;
         MainList[0].SubList[0].SubMin = 40f;
         MainList[0].SubList[0].SubMax = 50f;
     }

     void Update()
     {
         // Do Nothing    
     }
 }
Comment
Add comment · Show 1
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 maccabbe · Jun 28, 2015 at 02:27 AM 0
Share

Arrays of classes are just a group of references to objects. By default all there references are pointing to null and no new objects are created. So you need to actually objects assign to the array after you instantiate them, i.e.

  $$anonymous$$ainList[0]=new $$anonymous$$ainClass();

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DiGiaCom-Tech · Jun 28, 2015 at 03:31 AM

Thank you!!! ... I knew it was going to be something simple ... Face«Palm ;)

For the record ... your solution worked flawlessly!

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

21 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

Related Questions

Customize arrays and classes in inspector window 2 Answers

adding classes to arrays 2 Answers

Drag and Drop onto Array of Custom Objects 0 Answers

Using arrays in inspector variables 3 Answers

Can I preserve the values in a parent class when I overwrite it with a child 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