- Home /
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
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();
}
}
}
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();
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.
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();
}
}
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
Follow this Question
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