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 V4mpy · Oct 15, 2013 at 05:14 PM · c#array

Initialize Array custom datatype

Hello,

in my project, I have this in my DialogManager.cs :

 [System.Serializable] 
 public class DialogNode {
             
 public int id;
 public string text;
 public List<Option> options;
 
 }


and this:

 [System.Serializable]
     public class Option{
         
         public string text;
         public int link;
     }



Now I want to create an Array of these DialogNode:

public DialogNode[] dialogs;

So far, so good. This works perfectly and I can edit the size of this Array in the inscpector. BUT: I want to define a size of this array in

 void Start(){
     dialogs = new DialogNode[5]; // Error occurs here
         }




NullReferenceException: Object reference not set to an instance of an object

I know, WHY this error occurs, but I have no solution to fix it.

For interested parties:

I created a DialogManager and parsing a XML-File to my Array of DialogNodes. But I want a fix size of my Array.

Can anybody please give a hint?

Greetings,

V4mpy

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
1
Best Answer

Answer by V4mpy · Oct 17, 2013 at 12:09 PM

This works fine.

 public DialogNode[] dialogs;
         
             [System.Serializable]
             public class DialogNode
             {
                     
                 public int id;
                 public string text;
                 public Option[] options;
                 
                 public DialogNode ()
                 {
                     
                     this.id = 0;
                     this.text = "";
                 }
                 
         
             }
             
             [System.Serializable]
                 public class Option
             {
         
                 public string text;
                 public int link;
             
                 public Option ()
                 {
                     this.text = "";
                     this.link = 0;
                 }    
                 
             }
         
             void Start ()
             {
               
                 
                 
                 dialogs = new DialogNode[10];
                 for(int i = 0; i < 10; i++){
                     dialogs[i] = new DialogNode();
                     dialogs[i].options = new Option[10];
                     for(int x = 0; x < 10;x++){
                         dialogs[i].options[x] = new Option();
                     }
                 }
         
         }
 
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 Jamora · Oct 15, 2013 at 08:50 PM

Initializing the array like that will set all the values to their default, which is null for reference types.

You just need to loop over your newly initialized array and create a new instance of DialogNode in each element.

 for(int i=0;i<dialogs.Length;i++)
     dialogs[i] = new DialogNode();
Comment
Add comment · Show 3 · 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 V4mpy · Oct 15, 2013 at 09:11 PM 0
Share

Thanks for your reply. I could found this by myself. BUT: $$anonymous$$y Options in my DialogNode is also a List. I tried this additionally (I changed my Options to a array):

 for(int i = 0; i < 2; i++){
             dialogs[i] = new DialogNode();
         for(int x = 0; x < 5; x++){
             dialogs[i].options[x] = new Option();
         }
         }


Later in my code, I want to access on my options:

 dialogs[id].options[optionsID].text = myReader.ReadString();


Same Exception in this line above.

avatar image Jamora · Oct 16, 2013 at 11:59 AM 0
Share

Are you sure myReader is not null?

I would use the constructor of DialogNode to initialize any arrays within that class; afterall, that is what a constructor should do: construct the object so it's ready to use. You can overload the constructor too.

 //the constructor of DialogNode
 public DialogNode(int newID, string newText){
     InitializeOptions();
     id = newID;
     text = newText;
 }
 //overloaded constructor
 public DialogNode(int newID){
     InitializeOptions();
     id = newID;
     text = "";
 }
 
 private void InitializeOptions(){
     options = new Options[5];
     for(int x = 0; x < 5; x++){
         options[x] = new Option();
     }
 }
avatar image V4mpy · Oct 16, 2013 at 12:33 PM 0
Share

myReader is not null. The code works pretty fine, if I set the size of these arrays in the inspector.

The options are in my DialogNode. So I have to construct a DialogNode first and THAN the options, for this DialogNode..is that correct?

     [System.Serializable]
     public class DialogNode {
             
         public int id;
         public string text;
         public Option[] options;
         
         public DialogNode(){
             
             this.id = 0;
             this.text = "";
         }
         
 
 }
     
     [System.Serializable]
     public class Option{
 
         public string text;
         public int link;
     
     public Option(){
             this.text = "";
             this.link = 0;
         }    
         
     }

I want to construct both DialogNodes and options.

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

A node in a childnode? 1 Answer

Creating an array of gui styles and using them 1 Answer

C# Array Issue 2 Answers

C# SetActive GameObject Array 2 Answers

Unity 3 GD HotShot-Tutorial Problem with an C#-Array 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