- Home /
Does using try and catch actually fix the problem?
Hi. I'm getting a Null Exception error. I know why it's happening, I think. but that's not my question.
so I was sick of seeing the error, so I applied a Try and Catch. like this.
void DetectMoveableObject(){
if (moveableObjectFound == false && Input.GetButton ("Square")) {
try {
moveableObject = PushPullObjectDetection ();
}
catch (NullReferenceException ex) {
moveableObject = null;
}
this stops the error from getting thrown in the console, and the game runs fine in the editor during test play. does this mean the problem isn't a problem anymore? i know the error doesn't get thrown, but is the problem not a problem anymore? does catch actually fix the problem, or just make the error message go away? will it stop a build from compiling?
Answer by IvovdMarel · Nov 04, 2018 at 08:16 PM
The error will no longer pop up, and your code will compile, but it definitely does not fix the problem. You're basically telling the program to 'try' and run this code, but if there is any error you will 'catch' it yourself. Generally in the catch statement you would put a Debug.LogError or Debug.LogWarning to ensure you're aware this situation is still happening.
If you get a null reference you have to find out why it is null - in this case, it must be somewhere in your PushPullObjectDetection function. If you don't do this, later on, unexpected things will happen (the game might not work as expected, you get no errors, it's almost impossible to find out what's causing it).
well the thing is, i expect it to come back null. the script runs like if you press square, you search for an object. if an object is found you grab it, it returns null if nothing is found. the error only occurs when you find the object, and then drop it immediately by mashing the input button. the script chains into multiple different methods.
when you say unexpected things will happen, do you mean when the script interacts with other scripts in the future? or do you mean that it will react differently than it is right now even if there was nothing new built off or around the script as it is now?
full code is as follows, maybe i'm just missing something glaringly obvious?
public GameObject PushPullObjectDetection ()
{
Collider[] moveableObjectColliders = Physics.OverlapSphere (this.transform.position, moveableObjectDetectArea);
float $$anonymous$$Distance = $$anonymous$$athf.Infinity;
Collider moveableObject = null;
foreach (Collider box in moveableObjectColliders) {
if (box.gameObject.tag == "$$anonymous$$oveable Object") {
moveableObjects.Add (box.gameObject);
float distance = Vector3.Distance (box.transform.position, this.transform.position);
if (distance < $$anonymous$$Distance) {
moveableObject = box;
$$anonymous$$Distance = distance;
} else if (distance > $$anonymous$$Distance) {
moveableObject = null;
$$anonymous$$Distance = distance;
} else if (distance == $$anonymous$$Distance) {
moveableObject = null;
$$anonymous$$Distance = distance;
}
}
}
if (moveableObjects.Count != 0) {
moveableObjectFound = true;
}
if (moveableObjectFound == false) {return null;}
if (moveableObjectFound == true ) {return moveableObject.gameObject;}
return null;
}
EDIT: I think I figured it out. i just had to clear the array before returning the gameObject. I know that I could have avoided using arrays completely, being that it's only a single gameObject, but I'd already been using the script and it was just a quick copy paste thing.