I wrote a Component that requires the ParticleSystem Component. When I add my Component to a GameObject where the ParticleSystem is missing, I would like that Unity displays an error and prevents my component from being added to the GameObject.
So far I only found the RequireComponent Attribute, which automatically adds the required component automatically, that’s not exactly what I’m looking for.
Does anyone know how to convince Unity to display an error-message and don’t add my component if any dependency is missing?
You can write a custom Editor script for you component and use the OnEnable() event to check for required components. Just put the editor script in the Editor folder and it will just work automatically.
[CustomEditor(typeof(MyComponent))]
public class MyComponentEditor : Editor
{
public void OnEnable()
{
var t = target as MyComponent;
if (t && t.gameObject && t.GetComponent<ParticleSystem>() == null)
{
Debug.LogError("There is no ParticleSystem attached to " + t.gameObject.name);
}
}
}
To accomplish, I use an assertion method that will LogError (make sure you have pause on error enabled, of course), but also gets stripped out in release.
#define DEBUGUTILS_ENABLED
using UnityEngine;
using System.Diagnostics;
using Debug=UnityEngine.Debug;
public class DebugUtils
{
public static bool Enabled = true;
[Conditional("UNITY_EDITOR")]
public static void Assert(bool condition, string message="Assertion failure!")
{
#if UNITY_EDITOR && DEBUGUTILS_ENABLED
if (Enabled) {
if (!condition) {
UnityEditor.EditorApplication.Beep();
Debug.LogError(message);
}
}
#endif
}
}
Then, to use this, you can do the following:
void Start()
{
SomeComponent myComp = GetComponent<SomeComponent>();
DebugUtils.Assert(myComp != null, "We need a SomeComponent attached for this script to work!");
}
When you do a release build, two things happen:
The body of the DebugUtils.Assert method is stripped out because of conditional compilation.
Any calls to DebugUtils.Assert are removed when it’s compiled because of [Conditional(“UNITY_EDITOR”)]. You can check the IL yourself.
Yes, the DebugUtils class will still be in the final build, but it will just be a skeleton of the class. Just empty methods. I’m willing to deal with that in order to always have this available to me for debugging. I also put alternate Debug.Log methods in here to quickly test if my AI code truly is god awful slow, or it’s just the debug traces eating up cycles.
The best thing you can do is using something like this:
The problem is you can’t delete an object in it’s Reset function, because Reset is called from the internal constructor. So the Object can’t be destroyed inside the constructor.
That’s why i used a second helper script which removes the component. In your case you would have to implement Reset like this: