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
0
Question by lukefreemanb · Sep 28, 2020 at 08:48 AM · scriptableobjectcustom editor

Custom Inspector on scriptable object not displaying list elements until after I update script

I'm trying to make a type of scriptable object that acts as a 'base' for items to be generated with in my game. For the sake of keeping everything nicely tied together, I wanted to manage the creation of different types of items with one 'item' class. Basically have an enum change what parameters display in the inspector. This was all working fine when the itemBase class inherited from monodevelop, but once I changed it to a scriptable object so I could have different item configurations saved I started having issues. The 'mods' List is now giving me nullReferenceExceptions when I'm trying to reference it in my custom editor. Not sure why?

Here's my code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 [System.Serializable]
 [CreateAssetMenu(fileName = "item base", menuName = "Items/itemBase", order = 1)]
 public class itemBase : ScriptableObject
 {
     public enum Rarity { basic, uncommon, rare, artifact }
     public enum ItemType { weapon, armour, currency }
     public string itemName;
     public string description;
     public Rarity rarity;
     public ItemType itemType;
     
     //equipment (weapons/armour) only
     public bool canEquip;
     public int levelReq;
     public int carryWeight;
     public List<mod> mods;
     public int modCount;
     //weapons only
     public int minDmg, maxDmg;
     //armour only
     public int defence;
     //currency only
     public int quantity;
 }
 
 
 #if UNITY_EDITOR
 
 [CustomEditor(typeof(itemBase))]
 public class itemInspectorEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         itemBase enumScript = target as itemBase;
         var modsList = enumScript.mods;
 
         //Enum drop down
         enumScript.itemType = (itemBase.ItemType)EditorGUILayout.EnumPopup(enumScript.itemType);
         //default variables for all items
         enumScript.itemName = EditorGUILayout.TextField("Item Name", enumScript.itemName);
         enumScript.description = EditorGUILayout.TextField("Description", enumScript.description);
         enumScript.rarity = (itemBase.Rarity)EditorGUILayout.EnumPopup("Rarity", enumScript.rarity);
         
         switch (enumScript.itemType)
         {
             case itemBase.ItemType.weapon:
                 enumScript.carryWeight = EditorGUILayout.IntField("Carry Weight", enumScript.carryWeight);
                 enumScript.levelReq = EditorGUILayout.IntField("Level Requirement", enumScript.levelReq);
                 enumScript.minDmg = EditorGUILayout.IntField("min Damage", enumScript.minDmg);
                 enumScript.maxDmg = EditorGUILayout.IntField("max Damage", enumScript.maxDmg);
                 enumScript.modCount = Mathf.Max(0, EditorGUILayout.IntField("Possible Mods", enumScript.modCount));
                 while (enumScript.modCount < modsList.Count) //this is where I get the nullReferenceException
                 {
                     modsList.RemoveAt(modsList.Count - 1);
                 }
                 while (enumScript.modCount > modsList.Count)
                 {
                     modsList.Add(null);
                 }
                 for (int i = 0; i < modsList.Count; i++)
                 {
                     modsList[i] = (mod)EditorGUILayout.ObjectField(modsList[i], typeof(mod), false);
                 }
                 
                 
                 break;
             case itemBase.ItemType.armour:
                 enumScript.carryWeight = EditorGUILayout.IntField("Carry Weight", enumScript.carryWeight);
                 enumScript.levelReq = EditorGUILayout.IntField("Level Requirement", enumScript.levelReq);
                 enumScript.defence = EditorGUILayout.IntField("Defence", enumScript.defence);
                 enumScript.modCount = Mathf.Max(0, EditorGUILayout.IntField("Possible Mods", enumScript.modCount));
                 while (enumScript.modCount < modsList.Count)
                 {
                     modsList.RemoveAt(modsList.Count - 1);
                 }
                 while (enumScript.modCount > modsList.Count)
                 {
                     modsList.Add(null);
                 }
                 for (int i = 0; i < modsList.Count; i++)
                 {
                     modsList[i] = (mod)EditorGUILayout.ObjectField(modsList[i], typeof(mod), false);
                 }
                 break;
             case itemBase.ItemType.currency:
                 enumScript.quantity = EditorGUILayout.IntField("Quantity", enumScript.quantity);
                 break;
         }
 
     }
 }
 
 #endif
 
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

0 Replies

· Add your reply
  • Sort: 

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

139 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 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

CustomEditor for an ScriptableObject asset only works after recompile. 1 Answer

Draw scriptable object "inspector" in custom window (EditorWindow) 1 Answer

Why isn't my CustomEditor's OnInspectorGUI() running? 1 Answer

How to create custom editor for a class contained within another class 3 Answers

"Type mismatch" in Inspector for GameObject type in asset file 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