Require inspector assignment?

Was wondering if there was a way to require a variable be assigned via inspector, ala RequireComponent. I’m sure this has been asked plenty of times before, but all searches of Google and UA returned results about NullReferenceExceptions people were seeing because they forgot to assign something via inspector (which is exactly what I’m trying to prevent). I’m writing code that’ll be used by others, including non-technical folks, so leaving a comment like

// this var is assigned via inspector

doesn’t help much.

You could do Hexers answer but if its not assign make it so the launcher stops playing.

public GameObject ABC;
 
 if(ABC != null){
 //Line of code
 }
 else
 {
 Debug.LogError("assign the gameobject ABC in the inspector before resuming");
UnityEditor.EditorApplication.isPlaying = false;
 }

Hi @murkantilism, my response comes a little late, but I just saw this, I’m sorry. You have a way to make this much more comfortable and automatic.

After the end of your Class add this code:

[CustomEditor(typeof(*YOURCLASSNAME*), true)]
public class *YOURCLASSNAME*Inspector : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        *YOURCLASSNAME* myClass = (*YOURCLASSNAME*)target;

        if (myClass.yourVariable == null) myClass.yourVariable = yourValue;
    }
}

Replace YOURCLASSNAME with your Class Name and yourVariable/yourValue with your variable/value.

EDIT: Unity now knows to report the correct error in the Console, but does not stop play mode.

Your best bet would be to use the bailer idiom.

Extension method for convenience:

public static partial class GameObject_ExtensionMethods {
    public static void UnassignedReference(this GameObject go, string referenceName) {
        Debug.LogError("Unassigned Inspector reference: " + referenceName, go);
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#endif
        }
}

You must guard the last line, otherwise the project will not compile in Build.

private void Start() {
    if(myVar == null) gameObject.UnassignedReference(nameof(myVar));
    // Normal Start Stuff...
}

It is better to do these checks once in Start or Awake and proceed as normal than indent the code every time before use (bailer).
The problem with this solution is the code will continue to run for the rest of Starts or Awakes, even though we set the isPlaying to false, and this will most probably cause other NullReferenceExceptions down the line, which could confuse the user.

The helper class:

using UnityEngine;

internal static class MyDebug
{
    public static void Assert<T>(T variable) where T : class
    {
        Debug.AssertFormat(variable != null, "The instance of '{0}' is `null`.", typeof(T).Name);
    }
}

The usage:

public class Entity : MonoBehaviour
{
    public ExplosionDamage Damage;

    private void Awake()
    {
        MyDebug.Assert(Damage); // will print "The instance of 'ExplosionDamage ' is `null`."
    }
}

public GameObject ABC;

if(ABC != null){
//Line of code
}
else
{
Debug.LogError("assign the gameobject ABC in the inspector before resuming")
}

This code will pauze unity if ABC were to be null.

Or do :

If(ABC){
//line of code
}
else
{
    Debug.LogError("assign the gameobject ABC in the inspector before resuming")
}

Either of one should work.