Accessing Nested Class Information
So in my A class, I initialize an instance of class B. As part of class B's constructor, it initializes an instance of class C.
By using Debug.Log statements, I know that both B and C are being correctly initialized, however when I try to do B.C from within class A, I get a null reference exception, "Object Reference not set to an instance of an object".
Code is as follows:
public class A : Monobehavior
{
B b;
private void Awake()
{
b=new B("B_Name");
Debug.Log(b.c.CName);
//Null Reference Exception error happens on the above line of code
}
}
public class B
{
public C c;
public string BName;
public B(string name)
{
BName=name;
c=new C("C_Name");
}
}
public class C
{
public string CName;
public C("name")
{
CName=name;
}
}
Answer by WillHops · Jun 14, 2019 at 05:39 PM
Nvm, solved. I was doing "C c = new C" instead of just "c=new C". Whoops!
Your answer
Follow this Question
Related Questions
Custom class, Null Reference Exception 4 Answers
Nested class deserialization 2 Answers
cannot drag and assign other class from script to prefab 0 Answers
Way to copy a Class without copying the references? 0 Answers
Using variables from another script. 0 Answers