- Home /
RequireComponent, but don't automatically add it
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?
So, ins$$anonymous$$d of finding and automatically fixing the problem like RequireComponent would do, you want it to show an error message and fail? Why? What's so good about that?
Humans do mistakes and level designers click wrong buttons. I don't like the idea to automagically add dependencies in this case. I just want to display why it doesn't work.
@$$anonymous$$leptomaniac ps77 wants the system to fail fast https://en.wikipedia.org/wiki/Fail-fast
Answer by Soviut · Feb 19, 2013 at 02:42 AM
Unity has a MissingComponentException
in C# so I would simply throw an exception on Start/Awake/Enable if it's missing:
void OnEnable() {
if (GetComponent() == null) {
throw new MissingComponentException("Missing Camera component");
}
}
Answer by Tommynator · Mar 01, 2012 at 09:32 AM
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);
}
}
}
This won't help much. You can drag&drop components onto objects in the hierarchy panel, so the inspector isn't shown in this case.
Answer by michael-bartnett · Jul 18, 2012 at 08:30 PM
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.
Answer by Bunny83 · Mar 01, 2012 at 05:22 PM
The best thing you can do is using something like this:
http://answers.unity3d.com/questions/63826/replace-existing-component.html
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:
// This is C#:
#if UNITY_EDITOR
void Reset ()
{
Component comp1 = GetComponent<RequiredComponent1>();
Component comp2 = GetComponent<RequiredComponent2>();
if (comp1 == null || comp2 == null)
{
UnityEditor.EditorUtility.DisplayDialog("Dependencies missing ......", "This component needs a 'FooBar' ", "Ok");
gameObject.AddComponent<DeleteComponent>().componentReference = this;
}
}
#endif