- Home /
Question by
marx13maxz · Jun 20, 2018 at 02:27 AM ·
errorarraynullreferenceexceptionclassinitialization
C# custom class array error in adding a new entry.
[System.Serializable]
public class ButtonPair
{
public int AnsButton;
public string SelectButton;
}
Public Class MyClass : MonoBehaviour
{
ButtonPair[] ButtonPairs = new ButtonPair[4];
ButtonPairs[1].AnsButton = 10;
ButtonPairs[1].SelectButton = "test";
}
I have this code above and it is giving me a NullReferenceException
error. Please help. I am new to this class array.
Comment
Best Answer
Answer by marx13maxz · Jun 20, 2018 at 03:04 AM
Never mind. I found the answer in Question of @Common_Horse which is very similar to this. Kudos Unity Answer Citizens.
[System.Serializable]
public class ButtonPair
{
public int AnsButton;
public string SelectButton;
}
Public Class MyClass : MonoBehaviour
{
ButtonPair[] ButtonPairs = new ButtonPair[4];
ButtonPairs[1] = new ButtonPair(); //I inserted this line to solve the error
ButtonPairs[1].AnsButton = 10;
ButtonPairs[1].SelectButton = "test";
}