Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by DarkCSFixer · Apr 12, 2019 at 07:48 PM · inspectorhide

Hide null scripts in the inspector

I'm working on a game and I designed it so each character is given a script. The player holds items which also have scripts. It works something like this

 public class MyPlayer {
 
 int health;
 int mana;
 
 ItemInfo weapon;
 ItemInfo shield;
 
 }

ItemInfo is a script I made that has simple infomation in it. I use it for all my items. The problem is the character isn't always holding a weapon or shield, so I check to see if weapon == null. Normally this always works, but if I view MyPlayer in the inspector then it automatically creates a blank scripts for ItemInfo, making them not null... I could hide them, but I would like to be able to see them in they aren't null, for testing purposes.

As for my question, is there a way to hide null scripts in the inspector?

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 mchts · Apr 12, 2019 at 08:40 PM 0
Share

There is and it's custom inspector. Edit your script like this:

 public class $$anonymous$$yPlayer  : $$anonymous$$onoBehaviour{
     public int health;
     public int mana;
     
     [SerializeField] public ItemInfo weapon;
     [SerializeField] public ItemInfo shield;
 }

And put this class in a folder named Editor:

 [CustomEditor(typeof($$anonymous$$yPlayer))]
 public class $$anonymous$$yPlayerEditor : Editor {
 
     SerializedProperty weapon;
     SerializedProperty shield;
     
     private void OnEnable() {
         weapon = serializedObject.FindProperty("weapon");
         shield = serializedObject.FindProperty("shield");
     }
     
     public override void OnInspectorGUI() {
         var myPlayer = target as $$anonymous$$yPlayer;
         
         serializedObject.Update();
         
         myPlayer.health = EditorGUILayout.IntField("Health", myPlayer.health);
         myPlayer.mana = EditorGUILayout.IntField("$$anonymous$$ana", myPlayer.mana);
         
         //Show this item in the inspector only if it's not null 
         if(myPlayer.weapon != null){
             EditorGUILayout.PropertyField(weapon, GUIContent.none);
             serializedObject.Apply$$anonymous$$odifiedProperties();
         }
         if(myPlayer.shield != null){
             EditorGUILayout.PropertyField(shield, GUIContent.none);
             serializedObject.Apply$$anonymous$$odifiedProperties();
         }
     }
 }
avatar image DarkCSFixer mchts · Apr 13, 2019 at 12:18 AM 0
Share

Wow thanks, but there's a problem. It's seems that even if I hide the null script unity still makes a instants of it if I look at the class it's attached to.

avatar image mchts DarkCSFixer · Apr 13, 2019 at 04:31 AM 0
Share

It's not a problem, it's how it's meant to be. Yea there is a reference to that fields but it will be a null reference unless you don't assign anything to it. Editor has nothing to do with class itself it just manipulates how your class is being rendered. Your ItemInfo fields will always be there but they won't be rendered to inspector as long as they are null.

avatar image PizzaPie · Apr 13, 2019 at 12:14 PM 0
Share

If you want a non Unity nullable type to be null, do not serialize it. And by serialize it, I mean make it visible on inspector. Each time Unity Editor Updates (not game update) a serialization/ deserialization will occure for all objects, now Unity doesn't like null so it will replace it with an actual instance, so once it will deserialize the object, you no longer have a null, ex. try this

 [System.Serializable]
 public class $$anonymous$$SerializableClass
 {
     public int mInt;
 }
 
 public class $$anonymous$$Behaviour : $$anonymous$$onoBehavirour{
 public $$anonymous$$SerializableClass mClass = null;
 
 void Start(){
   mClass.mInt = 2; //this will not throw an error!
   mClass = null;
   StartCoroutine($$anonymous$$Routine());
   }
 
 IEnumerator $$anonymous$$Routine()
     {
         yield return new WaitForSeconds(1);
         mclass.mInt = 3;      //this will not throw an error!
     }
 

Anyhow the above code will not work in a build.

Cheers

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Apr 13, 2019 at 04:57 PM

That's not possible for custom serializable classes if you want them to be serialized and therefore visible in the inspector. The serialization system treats custom serializable classes like structs. So the inspector will always automatically create an instance of those classes. That's also the reason why self and circular references are not supported.


Make sure you read the documentation about script serialization carefully. Especially the part about custom serializable classes which starts at the line:


When might the serializer behave unexpectedly?


If you actually want references to classes, those classes need to be derived either from MonoBehaviour (and attached to a gameobject) or from ScriptableObject. In this case they are seperate objects and the field in the inspector doesn't show the class fields inline but just a reference to the referenced object.

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

107 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a minor change to Default inspector? 1 Answer

Variable lagging inspector even while hidden 1 Answer

Are inspector values supposed to override [HideInInspector] code? 1 Answer

Error while using SerializeField and HideInInspector tags 0 Answers

Displaying public variables in the inspector 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