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

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 *= new DialogNode();*

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 *= new DialogNode();*

_ dialogs*.options = new Option[10];_
_
for(int x = 0; x < 10;x++){_
_ dialogs.options = new Option();
}
}*_

}