01 logo

Unity3d - Interface null check issues

How to get a valid null check with Interfaces after calls to Destroy.

By Ricky Tucker JrPublished 3 years ago 3 min read
Like
Unity3d - Interface null check issues
Photo by Florian Olivo on Unsplash

Interfaces can be a useful tool when designing systems that need to expose the same set of Methods and Properties. This same behavior can be accomplished with inheritance through a deep or wide inheritance tree. Having a complex inheritance tree can become hard to manage. One example of this is making weighing the choice on weather this is the final branch to guard against fellow developers getting access to inside functionality and data of the class by inheriting from it. The reason why Interfaces could make it easier is it provide the structure and all classes defined could be closed, in the case of the developer worried about exploitation by inheritance.

When working with Interfaces in unity a common issue can introduce hard to find MissingComponentExceptions. When different objects that implement the interface are needed in one list using the interface as the Generic type of the list becomes essential. This will lead to the need to check if it has been properly assigned. This would of course be handled with a null check. How unity works with objects lead to a common error with interfaces. If an object is destroyed with the GameObject.Destroy method, it will assign a pseudo null value which is different than null, and a null check will return false;

ISearchable searchObject;

Debug.Log(searchObject == null); // This would output true

searchObject = new SearchableObject();

Destroy(searchObject);

Debug.Log(searchObject == null); // This would output false

The reason for this difference in results is unity uses null as not assigned or set to null and then a special pseudo null value, when Destroyed with unity Destroy Method, that is not equal to null

There are two simple solutions to this problem, and they are as follows.

Solution 1:

ISearchable searchObject;

Debug.Log(searchObject == null); // This would output true

searchObject = new SearchableObject();

Destroy(searchObject);

searchObject = null;

Debug.Log(searchObject == null); // This would output true

This is one of the easier to implement but also just as easy to forget and hard to pinpoint MissingReferenceException.

Solution 2:

ISearchable searchObject;

Debug.Log(searchObject == null); // This would output true

searchObject = new SearchableObject();

Destroy(searchObject);

//The if statement would equate to false and else statement would be //executed

if( (searchObject as UnityEngine.Object)

{

Debug.Log("Is a valid object");

}

else

{

Debug.Log("Is a null object");

}

Debug.Log(searchObject == null); // This would output true

This is a solution that is a little less elegant but would be easier on debugging. This is also the solution I choose on my personal project because it can easily be refactored as a method, an example of that method is below.

The refactored method would look like

public bool IsNull(ISearchable searchableItem)

{

return !(searchableItem as UnityEngine.Object);

}

public bool IsNotNull(ISearchable searchableItem)

{

return (searchableItem as UnityEngine.Object);

}

If this article helped you in your development endeavors, please give it a like. Also consider donating either through this platform or PayPal @ paypal.me/devricky to help me afford to spend more time creating articles like this one. I hope to continue to spread my knowledge as a software developer to other developers. Question can be sent to my email address [email protected]

I am a software engineer with over 5 years of development experience with Unity. I worked with a game startup DragonScale Studios. I am currently developing with MartinFederal. We use Unity to create simulations for the military for training purposes. This is an example of how a game engine can be used in so many projects. It is used in casinos to create the slots. It can be used for creating simulations. It can publish content to most consoles, and to web pages.

how to
Like

About the Creator

Ricky Tucker Jr

I will be feeling my stories with amazing adventures fueled by all the intangible ideas that float through my mind.

You can leave comments, help shape my adventures or buy me a coffee at https://www.buymeacoffee.com/rickytuckerjr

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.