Search Unity

[SOLVED] <cycle> displayed next to a field.

Discussion in 'Scripting' started by Deleted User, Nov 14, 2015.

  1. Deleted User

    Deleted User

    Guest

    In the Inspector, when I set the _default field in the StateController class (see below) to a State with StateType.Null (StateType is just an enum), the StateController field "_current" will display <cycle> in the Inspector and won't let me choose what kind of State I want. When I state the _default field to a State with a StateType other than null, I will be able to set _current whatever I want.

    Why is the Unity Inspector showing <cycle> for _current's value when _default's value is a State with StateType.Null?

    State:
    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4.     [Serializable]
    5.     public class State
    6.     {
    7.         [SerializeField] private readonly StateType _type;
    8.  
    9.         public StateType Type { get { return _type; } }
    10.  
    11.         public bool Equals(State other)
    12.         {
    13.             return _type == other._type;
    14.         }
    15.  
    16.         public override bool Equals(object obj)
    17.         {
    18.             if (ReferenceEquals(null, obj))
    19.             {
    20.                 return false;
    21.             }
    22.             return obj is State && Equals((State) obj);
    23.         }
    24.  
    25.         public override int GetHashCode()
    26.         {
    27.             return (int) _type;
    28.         }
    29.  
    30.         public static bool operator ==(State state1, State state2)
    31.         {
    32.             return state1.Equals(state2);
    33.         }
    34.  
    35.         public static bool operator !=(State state1, State state2)
    36.         {
    37.             return !state1.Equals(state2);
    38.         }
    39.     }
    StateController:
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3.  
    4.     [DisallowMultipleComponent]
    5.     public class StateController : Component
    6.     {
    7.         [SerializeField] private Animator _animator;
    8.         [SerializeField] private State _default;
    9.         [SerializeField] private State _current;
    10.         [SerializeField] private StateToAnimationMap _stateToAnimationMap;
    11.         public State Default { get { return _default; } }
    12.         public State Current { get { return _current; } }
    13.  
    14.         private void OnEnable()
    15.         {
    16.             SetCurrent(_current);
    17.         }
    18.  
    19.         public void SetCurrent(int index)
    20.         {
    21.             int current = 0;
    22.             foreach (var kvp in _stateToAnimationMap.Model)
    23.             {
    24.                 if (current != index)
    25.                 {
    26.                     current++;
    27.                     continue;
    28.                 }
    29.                 SetCurrent(kvp.Key);
    30.                 break;
    31.             }
    32.         }
    33.  
    34.         public void SetCurrent(State state)
    35.         {
    36.             if (!_animator.isActiveAndEnabled)
    37.             {
    38.                 Debug.LogWarning("Animator is not ready yet.");
    39.                 return;
    40.             }
    41.             IAnimationPlayer animation;
    42.             _current = !_stateToAnimationMap.Model.TryGetValue(state, out animation) ? _default : state;
    43.             PlayAnimationForCurrentState();
    44.         }
    45.  
    46.         private void PlayAnimationForCurrentState()
    47.         {
    48.             _stateToAnimationMap.Model[_current].PlayAnimation(gameObject, _animator);
    49.         }
    50.     }
     
  2. Deleted User

    Deleted User

    Guest

  3. Deleted User

    Deleted User

    Guest

    I'm thinking it has something to do with the way I overrided the equality methods in the State class since making it a struct fixes the issue. Anyone?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Not sure what might be going on, except perhaps some unintentional recursion. It's possible Unity's inspector detects some weird condition like a cyclic reference and declines to call it. Not sure how it might do that, but there might be stack trace decorators in C# that let you control how deeply something recurses and catch it before it locks up.

    That's just a guess. Try debugging your code and seeing what goes where.

    To understand recursion, you must first understand recursion.
     
    mukarramsc and Deleted User like this.
  5. Deleted User

    Deleted User

    Guest

    Thanks :p
     
  6. Deleted User

    Deleted User

    Guest

    Bump. Really curious as to why my code is recursive.
     
  7. Deleted User

    Deleted User

    Guest

    Bump. Anyone?
     
  8. Deleted User

    Deleted User

    Guest

  9. Deleted User

    Deleted User

    Guest

    Come on I know someone knows why this is recursive! ;P
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. public void SetCurrent(int index)
    3.         {
    4.            ...
    5.                 SetCurrent(kvp.Key);
    6.            ...
    7.         }
    8.      
    9.  
    recursion (assuming key is an int... )
     
    Kurt-Dekker and Deleted User like this.
  11. Deleted User

    Deleted User

    Guest

    Thanks I hadn't even seen that!