Use of "HelpBox" in Custom Inspector | Editor Scripting | C# | Unity Game Engine


PlayerScript.cs
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    public int health = 100;
    public int maxHealth = 100;
}

PlayerScriptEditor.cs
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(PlayerScript))]
public class PlayerScriptEditor : Editor
{
    public override void OnInspectorGUI()
    {
        PlayerScript playerScript = (PlayerScript)target;
        playerScript.health = EditorGUILayout.IntField("Health", playerScript.health);
        playerScript.maxHealth = EditorGUILayout.IntField("Max-Health", playerScript.maxHealth);

        if (playerScript.maxHealth > 0)
        {
            if(playerScript.maxHealth < playerScript.health)
                EditorGUILayout.HelpBox("Max Health is smaller than Health", MessageType.Warning);
        }
        else
        {
            EditorGUILayout.HelpBox("Max Health should be greater than 0", MessageType.Error);
        }
    }
}