There are a variable in class A.
In the class B creates an public instance of class A.
In the class C I can’t access public instance from class B.
Why so?
ClassA:
int x;
public int X {
get { return this.x; }
}
// Use this for initialization
void Start () {
x = 5;
}
ClassB:
public GameObject abc;
public ClassA classA; // it is public
void Start () {
classA = abc.GetComponent<ClassA> ();
}
void Update () {
Debug.Log ("Class B:" + classA.X);
}
ClassC:
void Update () {
// I have error here
// The name `classA' does not exist in the current context
Debug.Log ("Class B:" + classA.X);
}
ClassC doesn’t know what classA is. Only ClassB has the reference.
ClassC would need a public reference just like ClassB
ClassC:
public ClassA classA; // it is public
void Update () {
Debug.Log ("classA.X: " + classA.X);
If you’re trying to access ClassB’s reference of ClassA, then ClassC will need a reference of ClassB:
ClassC:
public ClassB classB; // it is public
void Update () {
Debug.Log ("classB.classA.X: " + classB.classA.X);
This is a general programming question, not really anything to do with Unity.