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
1
Question by Ant0nin · Aug 28, 2016 at 10:47 AM · scriptableobjecteditorguipropertydrawer

Custom PropertyDrawer fails because my ScriptableObject property is not initialized in time

Hi! I try to made a custom property drawer to have a 2D grid in the inspector (I need a 3D grid actually, like a voxel buffer).

I have 3 ScriptableObjects :

  • Level,

  • TileGrid,

  • Tile,

Each level has a reference to a tilegrid. I want the grid displaying in the inspector on level '.asset' file selection, so I made a custom PropertyDrawer related to TileGrid class.

My issue is that, when my OnGUI method is executed, an error raises when the program trying to access to a serialized property of tilegrid instance: the error say that there is no tilegrid instance. In my opinion, this is because my level instance has no correct default tilegrid value (null).

I noticed that, when I select a '.asset' level file in Unity Editor, the OnGUI function is executed before the OnEnable method of TileGrid (this is where I initialize the default tilegrid value and I don't see how to do it differently). Is someone has a solution about this?

EDIT: Confusion of mine, OnEnable is called only on object load. So how to define default ScriptableObject properties values inside an other ScriptableObject ? Is it even possible ?

Tile :

  [System.Serializable]
 [CreateAssetMenu (menuName ="Gamedata/Tile")]
 public class Tile : ScriptableObject 
 {
     public string meshFile;
     public string textureFile;
 }

TileGrid :

  [System.Serializable]
     public class TileGrid : ScriptableObject
     {
     public int dimX = 4;
     public int dimY = 4;
     private int dimZ = 1;
 
     public Tile[,,] tiles;
 
     void OnEnable()
     {
         if(tiles == null)
         {
             tiles= new Tile[dimX, dimY, dimZ];
             for(int z =0; z<dimZ; z++) {
                 for(int y = 0; y<dimY; y++) {
                     for(int x = 0; x<dimX; x++) {
                         tiles[x, y, z] = ScriptableObject.CreateInstance<Tile>();
                     }
                 }
             }
         }
     }
 }

Level:

  [CreateAssetMenu(menuName = "Gamedata/Level")]
     public class Level : ScriptableObject {
     
         public TileGrid grid;
 
 void OnEnable()
 {
         if (grid == null)
             grid = ScriptableObject.CreateInstance<TileGrid>();
 }

Custom PropertyDrawer:

 [CustomPropertyDrawer(typeof(TileGrid))]
 public class TileGridProperty : PropertyDrawer
 {
     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
     {
         EditorGUI.PrefixLabel(position, label);
 
         int dimX = property.FindPropertyRelative("dimX").intValue; // ERROR occurs
         int dimY = property.FindPropertyRelative("dimY").intValue;
         int dimZ = property.FindPropertyRelative("dimZ").intValue;
 
         position.y += 18f;
         position.width /= dimX;
         position.height /= dimY;
 
         SerializedProperty tileGrid = property.FindPropertyRelative("tileGrid");
         for(int z=0; z < dimZ; z++) {
             SerializedProperty arrayZ = tileGrid.GetArrayElementAtIndex(z);
             for(int y=0; y < dimY; y++)  {
                 SerializedProperty arrayY = arrayZ.GetArrayElementAtIndex(y);
                 for(int x=0; x < dimX; x++) {
                     SerializedProperty arrayX = arrayY.GetArrayElementAtIndex(x);
 
                     SerializedProperty tile = arrayX.GetArrayElementAtIndex(0);
                     EditorGUI.PropertyField(position, tile, GUIContent.none);
                 }
             }
         }
     }
 
     public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
     {
         return 18f;
     }
 }
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
3

Answer by lindsaytalbot · Jun 20, 2017 at 03:19 PM

For anyone else who finds this issue

SerializedObject obj = new SerializedObject(property.objectReferenceValue); var prop= obj.FindProperty("propName");

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 AubreyH · Sep 27, 2018 at 02:23 PM 0
Share

Life saver. Thank you. I've been converting serialized classes into scriptable objects, and could not for the life of me figure out why my PropertDrawers for those classes were broken. This fixes things now! Thanks!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnInteractivePreviewGUI multiple selection. Works but spams errors. 0 Answers

Applying a PropertyDrawer to elements of a List<> 1 Answer

ScriptableObject drawing error 0 Answers

Draw on Multiple Lines with EditorGUI 1 Answer

GetPropertyHeight infinite recursion on drawer 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