• 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 /
  • Help Room /
avatar image
0
Question by nicoguerra1337 · Jan 10, 2017 at 03:54 PM · c#editor-scriptingnullreferenceexceptionpropertydrawer

Property Drawer Doesn't Show Property

Hello everyone. I'm trying to create a property drawer to show stats for my game so I can edit them outside of code. Each stat is an instance of the BaseStat class. I already have a custom editor working to show the list of stats, but each element does not show the properties it is supposed to. Sometimes it just doesn't show, but other times it gives me a null reference exception. Here is the BaseStat class: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class BaseStat {
 
   public List<StatBonus> BaseAdditives { get; set; }
   public int BaseValue { get; set; }
   public string StatName { get; set; }
   public string StatDescription { get; set; }
   public int FinalValue { get; set; }
 
   public BaseStat (int baseValue, string statName, string statDescription) {
     this.BaseAdditives = new List<StatBonus>();
     this.BaseValue = baseValue;
     this.StatName = statName;
     this.StatDescription = statDescription;
   }
 
   public void AddStatBonus(StatBonus statBonus) {
     this.BaseAdditives.Add(statBonus);
   }
 
   public void RemoveStatBonus(StatBonus statBonus) {
     this.BaseAdditives.Remove(this.BaseAdditives.Find(x => x.BonusValue == statBonus.BonusValue));
   }
 
   public int GetCalculatedStatValue() {
     this.FinalValue = this.BaseValue;
     this.BaseAdditives.ForEach(x => this.FinalValue += x.BonusValue);
 
     return FinalValue;
   }
 
 }

I'm not sure if it doesn't work because it is a C# property and not a variable, but here is the property drawer script that I currently have set up to display the StatName property only:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [CustomPropertyDrawer(typeof(BaseStat))]
 public class BaseStatDrawer : PropertyDrawer {
 
   public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
     Rect contentPosition = EditorGUI.PrefixLabel(position, label);
     EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("StatName"), GUIContent.none);
   }
 
 }

Any other code improvement suggestions would also be helpful. Thanks in advance!

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
0

Answer by Adam-Mechtley · Jan 13, 2017 at 10:56 AM

Hi! Is your BaseStat class marked with System.Serializable?

Comment
Add comment · Show 2 · 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 nicoguerra1337 · Jan 28, 2017 at 02:23 PM 0
Share

Sorry for the late reply. I haven't worked on this project since I asked the question. BaseStat was not marked as System.Serializable, and marking it as System.Serializable had no effect. I still get a NullReferenceException on this line: EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("StatName"), GUIContent.none); I still think it might have something to do with StatName being a property and not a variable, but I have yet to find a solution for this.

avatar image Adam-Mechtley nicoguerra1337 · Jan 29, 2017 at 11:17 AM 0
Share

Can you edit your original post to include the actual stack trace?

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

8 People are following this question.

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

Related Questions

Unity Overlap Point Causing Error 0 Answers

NullReferenceException when trying to find tags with RaycastHit 1 Answer

Easy demand but unsolvable?? Set a permanent size (width and height) of a Texture even on different sized objects (c#) 0 Answers

Need quick editor scripting help 1 Answer

How to Have a ReoderableList in a Custom Editor Window 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