- Home /
Checking nulls almost all the time! Is this normal?
The script I wrote below checks the state of a static variable "selectedObject" from another class, every time I want to access or change things inside this static variable, I have to check the if it is null to make sure the null error does not pop out.
I have written most of my scripts in this method and thought maybe there could be a better design for this. Any pointers or advice is much appreciated!
if(myScript.selectedObject != null && (myScript.instance.notDead || Interface.selectedObject.fainted))
{
myScript.selectedObject.CheckState();
}
if(powerScript != null && powerScript.addPower < 0f)
{
powerScript.addPower = 10f;
}
if(myScript.selectedObject != null && myScript.selectedObject.activates)
{
Destroy(myScript.selectedObject.activates.gameObject);
myScript.selectedObject.activates = null;
}
Answer by Nischo · Feb 23, 2017 at 01:42 PM
In your case it could be easier to rewrite your code that you can write an early out using
if(myScript.selectedObject == null)
return;
ensuring that the followed code can safely work with myScript
.
Thanks for this useful info! I am still learning C# through coding in Unity actually, for what I know about "return" is that it is used in loops, can it be used in normal functions as well?
So as per your suggestion I'm assu$$anonymous$$g I would write my code like this...
if(myScript.selectedObject == null)
{
return;
}
else
{
if(myScript.instance.notDead || Interface.selectedObject.fainted))
{
myScript.selectedObject.CheckState();
}
if(powerScript != null && powerScript.addPower < 0f)
{
powerScript.addPower = 10f;
}
if(myScript.selectedObject.activates)
{
Destroy(myScript.selectedObject.activates.gameObject);
myScript.selectedObject.activates = null;
}
}
$$anonymous$$ore like:
void Your$$anonymous$$ethod() {
if(myScript.selectedObject == null) {
return;
}
if(myScript.instance.notDead || Interface.selectedObject.fainted) {
myScript.selectedObject.CheckState();
}
if(powerScript != null && powerScript.addPower < 0f) {
powerScript.addPower = 10f;
}
if(myScript.selectedObject.activates) {
Destroy(myScript.selectedObject.activates.gameObject);
myScript.selectedObject.activates = null;
}
}
The distinction is that you don't have an unnecessary "else" that contains the entire rest of the function.
the return keyword is used to return a value from the function. So just the return keyword ends the function and nothing further in it is processed.
An alternate way to do it is to put the null check in the calling function, like:
void Calling$$anonymous$$ethod() {
if(myScript.selectedObject != null) {
Your$$anonymous$$ethod();
}
}
void Your$$anonymous$$ethod() {
if(myScript.instance.notDead || Interface.selectedObject.fainted) {
myScript.selectedObject.CheckState();
}
if(powerScript != null && powerScript.addPower < 0f) {
powerScript.addPower = 10f;
}
if(myScript.selectedObject.activates) {
Destroy(myScript.selectedObject.activates.gameObject);
myScript.selectedObject.activates = null;
}
}
I prefer this way, as it makes the logic a bit clearer to read.