Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by ps77 · Mar 01, 2012 at 08:46 AM · requirecomponent

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?

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Mar 01, 2012 at 08:48 AM 0
Share

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?

avatar image Kleptomaniac · Mar 01, 2012 at 08:49 AM 0
Share

Yes, exactly, why would you want to do this?

avatar image ps77 · Mar 01, 2012 at 12:45 PM 1
Share

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.

avatar image demented_hedgehog · Aug 14, 2015 at 12:25 AM 0
Share

@$$anonymous$$leptomaniac ps77 wants the system to fail fast https://en.wikipedia.org/wiki/Fail-fast

4 Replies

· Add your reply
  • Sort: 
avatar image
3

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");
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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);
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 01, 2012 at 05:05 PM 0
Share

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.

avatar image
1

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:

  1. The body of the DebugUtils.Assert method is stripped out because of conditional compilation.

  2. 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get types from UnityEngine.dll failed on RequireComponent 1 Answer

How to set the property of my required component? 2 Answers

Is it possible to use || (or) with RequireComponent? 5 Answers

Require Component for Parent 2 Answers

How to use RequireComponent with ParticleEmitters? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges