Hello all
Created new project. Made a cube and a sphere.
Created script AAA and attached it to Cube.
Created script BBB and attached it to Sphere
Class BBB:
public class BBB : MonoBehaviour {
public bool Test = true;
}
Class AAA:
public class AAA : MonoBehaviour {
private BBB bbb;
void Start ()
{
bbb = GetComponent<BBB>();
Debug.Log(bbb.Test);
}
}
Debug.Log trows NullReferenceException.
Why?
“bbb” needs something like
GameObject.FindObjectByTag(“Sphere”).GetComponent…
or make bbb decl public and drag/drop reference to Sphere in the Inspector field that will appear.
Tnx, but I was not clear.
I know how to make this work other ways.
My question was why is this not working.
I am C# beginner and in combination with Unity infrastructure it only gets complicated.
Both classes are public and in same Visual Studio project. I would like someone to try to explain to me why is this class “visible” by project and yet not accessible by other classes.