- Home /
NullReferenceException by trying to access another script.
Yes, I know NullReferenceException is a very common error and in most cases it is the same, but in this case I have not found anything about it on Google or Unity. Here is my first script:
void Update() {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (isSelected) {
if (currentBuilding != null && !hasPlaced) {
obj_Check scriptDisable = currentBuilding.GetComponent<obj_Check>();
if(Physics.Raycast(ray, out hit, Mathf.Infinity)) {
currentBuilding.position = new Vector3(hit.point.x, 0, hit.point.z);
if(Input.GetMouseButtonDown(0)) {
if (IsLegalPosition()) {
hasPlaced = true;
isSelected = false;
}
}
}
}
}
else {
if(Input.GetMouseButtonDown(0)) {
if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
if(hit.transform.gameObject.tag == "Placeable") {
pl = hit.transform.gameObject;
selectObj = pl.GetComponent<SelectionScript>();
selectObj.selected = true;
}
else {
if(pl != null) {
selectObj = pl.GetComponent<SelectionScript>();
selectObj.selected = false;
pl = null;
}
}
}
}
}
}
bool IsLegalPosition() {
if(checker.colliders.Count > 0) {
return false;
}
return true;
}
And here is my second script, obj_Check in which the first script references:
public List<Collider> colliders = new List<Collider>();
void OnTriggerEnter(Collider c) {
if (c.tag == "Placeable") {
colliders.Add (c);
}
}
void OnTriggerExit (Collider c) {
if (c.tag == "Placeable") {
colliders.Remove (c);
}
}
Now whenever I select my building or whatever, and try to place it down on the ground, it gives me this error: NullReferenceException: Object reference not set to an instance of an object gameController.IsLegalPosition () (at Assets/Scripts/gameController.cs:128) gameController.Update () (at Assets/Scripts/gameController.cs:99)
I have no idea why... Thanks in advance for your help!
Where does "checker" get set? That is most likely what is set to null. If it is supposed to be set on the script in the inspector, double check it.
We need to see the whole gameController.cs script. Otherwise we have no idea where those lines are.
The error is clearly(?) pointing to the IsLegalPosition() method, and there's only 2 things there that possibly could be null:
checker
or
checker.colliders
Your script doesn't show how you initialize the check
variable and whether it's set to null at some point.
Unless it's a very special case or a known bug in a code library that is being used, Null Reference Errors can seldom be solved by Googling other peoples problems. It's like Googling "what is this loud noise in my backyard" :) It easier to look yourself once you know how to.
One way to get started is to just have to look at the line the error is pointing to (double click the error in Unity), and for example Debug.Log()
the values of all the variables on that line to pin down the variable that is null. Then start tracking down why... :)
O$$anonymous$$. I figured it out. The cube was in an empty gameobject. The GA$$anonymous$$EOBJECT parent was the one that got instantiated.. I'm sorry xD