Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Benji_1996 · Sep 26, 2019 at 05:03 AM · guieditorguilayoutpropertydrawereditorguilayout

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer?

I am trying to create a CustomPropertyDrawer for a class I've designed called ComponentReference and I am running into some serious issues. I want to use the auto layout system to make things easier on myself (basically using static methods from "EditorGUILayout" instead of "EditorGUI") but this results in the Argument Exception listed in the subject line of this question.


As I understand, this exception is being thrown because there is a mismatch in the controls being laid out in the OnGUI function between function calls. The typical hypothesis I get from other questions on this forum is that the OnGUI function sets up some controls during the "Layout" event, but then something changes and the controls are different when called during the "Repaint" event. So in a segment of code like this:


private bool dropdown;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    foldout = EditorGUILayout.BeginFoldoutHeaderGroup(foldout, label);
    if (foldout)
    {
         EditorGUILayout.LabelField("Hello");
    }
    EditorGUILayout.EndFoldoutHeaderGroup();
}

One can easily imagine such a scenario occurring here where "foldout" evaluates to "false" when OnGUI is called for the Layout event, but then evaluates to "true" when OnGUI is called during the Repaint phase, which would throw an Argument Exception similar to the one listed in the subject line of this question.

I really believed that this was the case for my own code until actually outputting the event types as they were processed in the OnGUI function of my custom property drawer. Given this code:


public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    Debug.Log("Current frame num: " + Time.frameCount);
    Debug.Log("Current event type: " + Event.current.type);
    // Other code
}

The output I got looked like this:

Current frame num: 1079
Current event type: repaint
ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
Current frame num: 1080
Current event type: repaint
ArgumentException: Getting control 1's position in a group with only 1 controls when doing repaint
(and so on...)

It's also worth noting that some outputs occurred on the same frame, however, the only event type ever recorded was the "repaint" event. Based on the console outputs, the layout event for OnGUI was never even called for this custom property drawer, which I assume is an issue since I'm trying to use auto-layout.

I've done so much miscellaneous research into this and I'm completely exhausted. The Unity Manual and API is really miserable because it only ever shows examples of CustomPropertyDrawer with manual layouting which is just miserable and completely unacceptable. Here's all the code that makes it happen:

using UnityEngine;

public abstract class ComponentReference where TComponent : Component { [SerializeField] private ComponentReferenceType type; [SerializeField] private GameObject obj; [SerializeField] [TagSelector] private string objectTag; [SerializeField] private bool includeChildren; [SerializeField] private TComponent _component; public TComponent component { get { if (_component == null) { RefreshComponentReference(); } return _component; } } public void RefreshComponentReference() { switch (type) { case ComponentReferenceType.DropTarget: LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found. Did you forget to " + "set the component reference in the editor?", _component == null); break; case ComponentReferenceType.FindInScene: _component = Object.FindObjectOfType(); LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found " + "anywhere in the scene", _component == null); break; case ComponentReferenceType.GameObjectDropTarget: TryGetComponent("No Game Object found. Did you forget to set the Game Object " + "in the editor?"); break; case ComponentReferenceType.TagTarget: obj = GameObject.FindGameObjectWithTag(objectTag); TryGetComponent("No Game Object found with the tag " + objectTag); break; } } private void TryGetComponent(string gameObjectNullMessage) { if (obj != null) { _component = obj.GetComponent(includeChildren); LogErrorIf("No component of type " + typeof(TComponent).ToString() + " found on Game Object with name " + obj.name, _component == null); } else { Debug.LogError(gameObjectNullMessage); } } private void LogErrorIf(string message, bool condition) { if (condition) { Debug.LogError(message); } } }

public enum ComponentReferenceType { DropTarget, FindInScene, GameObjectDropTarget, TagTarget, }

[System.Serializable] public class TransformComponentReference : ComponentReference { }

[System.Serializable] public class CameraComponentReference : ComponentReference { }

// File: ComponentReferenceDrawer using UnityEditor; using UnityEngine;

public class ComponentReferenceDrawer : PropertyDrawer { private bool foldout = false; public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { Debug.Log("Current frame num: " + Time.frameCount); Debug.Log("Current event type: " + Event.current.type); foldout = EditorGUILayout.BeginFoldoutHeaderGroup(foldout, label); // ERROR OCCURS HERE if (foldout) { // Setup the type property SerializedProperty typeProperty = property.FindPropertyRelative("type"); EditorGUILayout.PropertyField(typeProperty); // Change the layout of the serialized properties // based on the enum selected switch (typeProperty.enumValueIndex) { // Property field for the component directly case (int)ComponentReferenceType.DropTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("_component")); break; // No property fields - the component reference will be located programmatically case (int)ComponentReferenceType.FindInScene: break; // Property field for the game object the component is attached to case (int)ComponentReferenceType.GameObjectDropTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("obj")); EditorGUILayout.PropertyField(property.FindPropertyRelative("includeChildren")); break; // Property field for a tag of a game object the component is attached to case (int)ComponentReferenceType.TagTarget: EditorGUILayout.PropertyField(property.FindPropertyRelative("objectTag")); EditorGUILayout.PropertyField(property.FindPropertyRelative("includeChildren")); break; } } EditorGUILayout.EndFoldoutHeaderGroup(); } } [CustomPropertyDrawer(typeof(TransformComponentReference))] public class TransformComponentReferenceDrawer : ComponentReferenceDrawer { } [CustomPropertyDrawer(typeof(CameraComponentReference))] public class CameraComponentReferenceDrawer : ComponentReferenceDrawer { }
Sorry if the code looks dumb. This little textbox is being so uncooperative when it comes to formatting the freaking text. Anyways, I'm exhausted so I appreciate anyone's effort to help me out with this

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

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by Ideka · Nov 14, 2019 at 09:37 PM

I found this always happens if:

  1. You use a custom PropertyDrawer that uses GUILayout or EditorGUILayout within OnGUI

  2. The associated Property decorates a field in a ScriptableObject

  3. The ScriptableObject has no custom Editor

The workaround I found was to just make a custom editor for the ScriptableObject, like so:

 [CustomEditor(typeof(Whatever), true)]
 public class WhateverEditor : Editor { }

And with that the error goes away.

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 jeongheon_lee_od · Mar 04, 2020 at 08:51 AM 1
Share

Thanks a lot, you save my time. WOW. Exactly same case.

avatar image laurG · Mar 06, 2021 at 01:53 PM 0
Share

Thanks! I had a similar issue, but for the MonoBehaviour and this fixed it

avatar image
2

Answer by idbrii · Jul 21, 2020 at 02:27 AM

I want to use the auto layout system to make things easier on myself (basically using static methods from "EditorGUILayout" instead of "EditorGUI") but this results in the Argument Exception listed in the subject line of this question.

I had the same problem and it turns out the documentation for PropertyDrawer says:

Note that for performance reasons, EditorGUILayout functions are not usable with PropertyDrawers.

It used to work for me back on 2018, but it seems that Unity's changed something on 2019 and using EditorGUILayout always causes this ArgumentException.

(Also, it looks like this warning was there even back on 5.6.)

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

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

(Solution) - Can't use GUILayout stuff in PropertyDrawer.OnGUI? 2 Answers

Get height of a group of GUILayout controls 1 Answer

BeginScrollView - But make background darker? 1 Answer

Space for describing text in EditorGUILayout too short 1 Answer

Custom Editor Struct array layout 0 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