• 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 gyrosp · Aug 01, 2015 at 03:31 AM · inspector

Show a warning in the inspector

I'd like to show a warning in the inspector like the Point Effector 2D does if there is no 2D Collider present on the same game object:

alt text

How to accomplish that?

3.png (44.4 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by Bunny83 · Aug 01, 2015 at 04:29 AM

You have to implement a custom inspector for your class and when you want to show such a warning you can use "EditorGUILayout.HelpBox" inside OnInspectorGUI. Something like that:

 EditorGUILayout.HelpBox("Some warning text", MessageType.Warning);


Comment
Add comment · Show 3 · 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 gyrosp · Aug 01, 2015 at 11:17 AM 0
Share

Thanks :).

Unfortunately it does not work as expected. I like to check if another script is present on the same game object. If it is not the warning should show.

I check for the script in the start()-method like this:

         health = gameObject.GetComponent<Health> ();
         Debug.Log ("Health: " + health);
         if (!_health)
             _scriptDependecyWarning = "Health script must be assigend to the same gameobject";

This is my editor-script:

 ...
         string dependencyWarning = damage$$anonymous$$anager.getScriptDependenyWarning();
 
         if (damage$$anonymous$$anager.health == null)
             EditorGUILayout.HelpBox("Warnung", $$anonymous$$essageType.Warning);
 ...

As it seems the start()-method is not called from the inspector what makes sense in a way. But how do I check for the presence of another script in design-time?

avatar image Bunny83 · Aug 01, 2015 at 11:36 AM 0
Share

You have to seperate editor code from in game code. You should check if the component is attached inside the editor script itself.

 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();
     YourRuntimeScriptName script = (YourRuntimeScriptName)target;
     Health tmp = script.GetComponent<Health>();
     if (tmp == null)
     {
         EditorGUILayout.HelpBox("Health script must be assigend to the same gameobject", $$anonymous$$essageType.Warning);
     }
 }

edit
Do you know the RequireComponent attribute? It will automatically add scripts which your script depend on to the same gameobject when your script is added to a gameobject in the editor. Just to make that clear: That attribute only affects the behaviour of the editor. When you use AddComponent from script to add your class to a gameobject it won't add those dependencies. Also if you already have the script attached to a gameobject and add the attribute to the script, it won't add the dependencies either. Only for newly added components.

All you have to do is add this attribute to your runtime script:

 [RequireComponent(typeof(Health))]
 public class YourRuntimeScriptName : $$anonymous$$onoBehaviour
 {
    // [...]
 }

Note: RequireComponent only works for concrete classes. That's why the "Point Effector 2D" component can't use it since it makes no sense to add "some kind" of collider. In your case it seems that you have one concrete dependency where the attribute can be used just fine. It just doesn't work with baseclasses since Untiy don't know which actual class it should attach.

avatar image gyrosp · Aug 01, 2015 at 12:18 PM 0
Share

It works like a sharm now and thanks for the hint with the RequireComponent attribute. I didn't know it, yet but I'll use it :).

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Customizing Inspector variables. 2 Answers

Using [SerializeField] vs public 2 Answers

Can I set default values for a nested class? 2 Answers

How to make builtin Component Inspector Settings available on Custom Component? 0 Answers

Custom Inspector, Access built-in component icons? 2 Answers


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