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): namespace Example001 { public class Example : MonoBehaviour { public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest } void Start() { //... } void Update() { //... } void MovePlayer( GameObject oPlayer, Direction eDirection = Direction.North ) { //... } } } Simply fixed by falling back on the older method of doing this, however it would be nice to have this feature: Code (csharp): namespace Example001 { public class Example : MonoBehaviour { public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest } void Start() { //... } void Update() { //... } void MovePlayer( GameObject oPlayer ) { MovePlayer( oPlayer, Direction.North ); } void MovePlayer( GameObject oPlayer, Direction eDirection ) { //... } } } Cheers, -G. P.S. The forum input text field is driving me crazy. It appears to have issues in IE11/IE10... ahhhhhh!
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): using UnityEngine; using System.Collections; public class NewBehaviourScript : MonoBehaviour { public enum Direction : int { North, South, East, West, NorthEast, SouthEast, SouthWest, NorthWest } // Use this for initialization void Start () { MovePlayer(new GameObject()); } // Update is called once per frame void Update () { } void MovePlayer( GameObject oPlayer, Direction eDirection = Direction.North ) { Debug.Log(eDirection); } }
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... -G.
It seems this has been posted as a bug, found this thread relating to the same problem. http://answers.unity3d.com/questions/361976/are-namespaces-supported-in-unity-4.html
If only I knew to search for the namespace... it would have saved me more time. Thanks for the update!
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): //... namespace CompanyName.Editor.Inspector { [RequireComponent( typeof( GUITexture ) )] [CustomEditor( typeof( GuiMap ) ), CanEditMultipleObjects] public class GuiMapInspector : ComGuiInspector { #region Styles private static GUIStyle s_oBackgroundStyle = null; #endregion #region Serialized Properties private SerializedProperty m_fWidthProperty = null; private SerializedProperty m_fHeightProperty = null; #endregion /// <summary> /// Called to initialize the instance of this inspector class /// </summary> public override void OnEnable() { base.OnEnable(); m_fWidthProperty = serializedObject.FindProperty( "m_fWidth" ); m_fHeightProperty = serializedObject.FindProperty( "m_fHeight" ); } /// <summary> /// Called to redraw GUI and react to user events /// </summary> public override void OnInspectorGUI() { base.serializedObject.Update(); base.OnInspectorGUI(); EditorGUILayout.SelectableLabel( "Layout:", GetBackgroundStyle(), GUILayout.ExpandWidth( true ) ) //... //... //... if( GUI.changed ) { base.serializedObject.ApplyModifiedProperties(); EditorUtility.SetDirty( base.target ); } } /// <summary> /// Retrieves the style used to render the background /// </summary> /// <param name="bRefresh">Whether or not to refresh the background style or use what's cached</param> /// <returns>A background GUI style</returns> private static bool GetBackgroundStyle( bool bRefresh = false ) { if( s_oBackgroundStyle == null || bRefresh ) { s_oBackgroundStyle = new GUIStyle( GUI.skin.box ); s_oBackgroundStyle.padding = new RectOffset( 3, 3, 3, 3 ); } return s_oBackgroundStyle; } } }