Search Unity

Script / Inspector Bug

Discussion in 'Scripting' started by DigitalGrunt, Feb 28, 2014.

  1. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    I've noticed a bug in the script code when creating behaviours that use scripts containing default method parameters. Unity will display a message in the inspector:
    If anything, it should warn you about the limitation as it took me some time to figure out what was going on.

    Example:
    Code (csharp):
    1.  
    2. namespace Example001
    3. {
    4.    public class Example : MonoBehaviour
    5.    {
    6.  
    7.      public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest }
    8.  
    9.      void Start()
    10.      {
    11.         //...
    12.      }
    13.  
    14.      void Update()
    15.      {
    16.           //...
    17.      }
    18.    
    19.      void MovePlayer( GameObject oPlayer, Direction eDirection = Direction.North )
    20.      {
    21.           //...
    22.       }
    23.   }
    24. }
    25.  
    Simply fixed by falling back on the older method of doing this, however it would be nice to have this feature:

    Code (csharp):
    1.  
    2. namespace Example001
    3. {
    4.   public class Example : MonoBehaviour
    5.   {
    6.        public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest }
    7.  
    8.        void Start()
    9.         {
    10.           //...
    11.        }
    12.    
    13.        void Update()
    14.        {
    15.           //...
    16.        }
    17.  
    18.        void MovePlayer( GameObject oPlayer )
    19.        {
    20.            MovePlayer( oPlayer, Direction.North );
    21.        }
    22.    
    23.        void MovePlayer( GameObject oPlayer, Direction eDirection )
    24.        {
    25.           //...
    26.         }
    27.    }
    28. }
    29.  

    Cheers,
    -G.

    P.S. The forum input text field is driving me crazy. It appears to have issues in IE11/IE10... ahhhhhh!
     
    Last edited: Feb 28, 2014
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    I just tried it, and I don't get any error in unity when using a default method parameter.

    This is what I tried, Debug.log prints 'North'

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.     public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest }
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.         MovePlayer(new GameObject());
    12.     }
    13.    
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.  
    19.     void MovePlayer( GameObject oPlayer, Direction eDirection = Direction.North )
    20.     {
    21.         Debug.Log(eDirection);
    22.     }
    23.    
    24.  
    25. }
    26.  
    27.  
     
  3. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    Forgot to add the namespace. I didn't realize it until after that it was a combination of the namespace and the default method parameter. Interesting...:cool:


    -G.
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
  5. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    If only I knew to search for the namespace... it would have saved me more time. Thanks for the update!
     
  6. DigitalGrunt

    DigitalGrunt

    Joined:
    Feb 25, 2014
    Posts:
    8
    To follow up on this issue earlier, I've also found a bug in the inspector classes which exhibit similar symptoms where I would get this error in the console:

    And this error in the inspector:

    Even through the file was working fine before. Again, it's due to adding the default parameter in the GetBackgroundStyle() method.
    Code (CSharp):
    1. //...
    2.  
    3. namespace CompanyName.Editor.Inspector
    4. {
    5.     [RequireComponent( typeof( GUITexture ) )]
    6.     [CustomEditor( typeof( GuiMap ) ), CanEditMultipleObjects]
    7.     public class GuiMapInspector : ComGuiInspector
    8.     {
    9.         #region Styles
    10.         private static GUIStyle s_oBackgroundStyle = null;
    11.         #endregion
    12.        
    13.         #region Serialized Properties
    14.         private SerializedProperty m_fWidthProperty = null;
    15.         private SerializedProperty m_fHeightProperty = null;
    16.         #endregion
    17.        
    18.         /// <summary>
    19.         /// Called to initialize the instance of this inspector class
    20.         /// </summary>
    21.         public override void OnEnable()
    22.         {
    23.             base.OnEnable();
    24.             m_fWidthProperty = serializedObject.FindProperty( "m_fWidth" );
    25.             m_fHeightProperty = serializedObject.FindProperty( "m_fHeight" );
    26.         }
    27.        
    28.         /// <summary>
    29.         /// Called to redraw GUI and react to user events
    30.         /// </summary>
    31.         public override void OnInspectorGUI()
    32.         {
    33.             base.serializedObject.Update();
    34.             base.OnInspectorGUI();
    35.            
    36.             EditorGUILayout.SelectableLabel( "Layout:", GetBackgroundStyle(), GUILayout.ExpandWidth( true ) )
    37.            
    38.             //...
    39.             //...
    40.             //...
    41.            
    42.             if( GUI.changed )
    43.             {
    44.                 base.serializedObject.ApplyModifiedProperties();
    45.                 EditorUtility.SetDirty( base.target );
    46.             }
    47.         }
    48.    
    49.    
    50.         /// <summary>
    51.         /// Retrieves the style used to render the background
    52.         /// </summary>
    53.         /// <param name="bRefresh">Whether or not to refresh the background style or use what's cached</param>
    54.         /// <returns>A background GUI style</returns>
    55.         private static bool GetBackgroundStyle( bool bRefresh = false )
    56.         {
    57.             if( s_oBackgroundStyle == null || bRefresh )
    58.             {
    59.                 s_oBackgroundStyle = new GUIStyle( GUI.skin.box );
    60.                 s_oBackgroundStyle.padding = new RectOffset( 3, 3, 3, 3 );
    61.             }
    62.            
    63.             return s_oBackgroundStyle;
    64.         }
    65.     }
    66. }