Giter VIP home page Giter VIP logo

unity-null-reference-detection's Introduction

Finding null references in Unity

alt text

This handy Unity plug-in helps you to find null references in your scene objects to avoid runtime exceptions. It's lightweight and both easy to install and to use.

Setup

If you want to get a quick overview you should check out the demo project. You can also download the package containing only the essential scripts.

After downloading and importing the package, you will find a new entry under Tools > Find Null References.

How to use

In the screenshot above there's a cube with a script containing three references. Here's the example code:

using NullReferenceDetection;
using UnityEngine;

public class CubeScript : MonoBehaviour {

    [SerializeField]
    //Serialized, private fields are visible in the inspector
    private SphereScript sphere;

    [ValueRequired]
    //Will be shown as red text if null
    public CapsuleScript capsule;

    [ValueOptional]
    //Won't be shown even if null
    public CylinderScript cylinder;
}

All fields that appear in the Unity Inspector will also be checked for null by the plug-in. That includes public fields and serialized private fields.

In addition you can use two custom attributes to further specify how your fields should be handled:

  • [ValueRequired] will print a red error message if null
  • [ValueOptional]won't show in the console even if null

Using these custom attributes is purely optional.

If you now execute the plug-in via Tools > Find Null References the console will show two error messages, like in the screenshot above. The capsule reference is marked as required, therefore it's show as red. sphere is shown with the default severity and cylinder is not shown at all as the [ValueOptional] attribute allows the field to be a null reference.

Project Settings

You can open the preferences window by navigating to Edit -> Project Settings -> Null Reference Detection where you can define which occurences of null references should be printed to the console. You can also change their text color.

If you want to automatically check for null references, you can do so by activating checks on entering play mode or on compilation. You can also prevent Unity from entering play mode if any null references were found.

alt text

Additionally in the preferences window you can define a list of GameObjects for which null references will be ignored, this is like the [ValueOptional] tag but affects the entire GameObject (and it's children, if the checkbox is ticked).

alt text

Finaly, by default the plug-in only scans GameObjects which are in the scene. If you have additional prefabs which are only instanciated at run-time, but you would like them to also be checked by the plug-in you can add the path to the prefab here.

alt text

Extension

By inheriting from the abstract BaseAttribute class you can implement your own attributes. They will also show up in the preferences window where you assign a color of your choice to your custom attribute.

Good to know

Clicking on an error message will highlight the target object in the Hierarchy, or if the target object is in a prefab it will highlight the folder in the Project panel.

The save files for the preferences are plain text files, aiding in ease of use so that you can easily see what was added or removed when staging files or working on a merge request etc.

The plug-in does not...

  • check list items for null references.
  • check for properties of default unity components (such as transform, box collider etc.)
  • consider objects in other scenes but the currently opened scene.
  • consider inactive objects.
  • automatically update the entry of a prefab if the filename or path changes

unity-null-reference-detection's People

Contributors

btwist avatar moolt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

unity-null-reference-detection's Issues

Add a check on compile / before play

Thank you for the amazing asset. This was exactly what I was looking for.
Question: is it easy to add a setting that null checks after script compilation or before entering play mode?

I'd prefer it to run automatically because I can forgot to click the menu icon ocassionally.

I'll give a try, might send a PR if it makes sense to you.

Thanks

Build errors

Hi. Thanks for making this asset. We tried to use it in our project, but ran into some build errors, which I think may be caused by using classes from the UnityEditor namespace outside of the Editor folder. Here's an image of the errors:
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.