EnumPopup changes value on play

I’m using an EnumPopup to select which control layout I should use for each player. Heres my setup:

public class PlayerInputController : MonoBehaviour
{
    public enum Player { Player1, Player2 };

    [SerializeField] public Player player;

    ...

    private void OnEnable() {
        if (player == Player.Player1) {
            EnablePlayer1Actions();
        } else if (player == Player.Player2) {
            EnablePlayer2Actions();
        }
    }

    ...
}

Here is my CustomEditor for the above Class:

using UnityEditor;

[CustomEditor(typeof(PlayerInputController))]
public class PlayerInputControllerEditor : Editor
{
    public override void OnInspectorGUI()
    {
        // base.OnInspectorGUI();
        PlayerInputController script = target as PlayerInputController;

        script.player = (PlayerInputController.Player)EditorGUILayout.EnumPopup("Player", script.player);

        EditorGUILayout.Space();

        if (script.player == PlayerInputController.Player.Player1) {
            // Show Player1Events
            ShowPlayerEvents("player1MoveInputEvent", "player1JumpEvent");
        } else if (script.player == PlayerInputController.Player.Player2) {
            // Show Player2Events
            ShowPlayerEvents("player2MoveInputEvent", "player2JumpEvent");
        }
    }

    void ShowPlayerEvents(string moveEvent, string jumpEvent) {
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty(moveEvent), true);
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty(jumpEvent), true);
    }
}

The issue I’m having is that when I run the game, the selection for Player1 switches to Player2, causing the Inputs to not be enabled. And vice-versa for Player2. Even if I run the game again after this switch occurs, the input stays on the incorrect one.

1 Answer

1

I found out that it was because this CustomEditor was applied to a script thats on my Player Prefab, and the values were being overwritten by the Prefab. To fix this I used a combination of SerializedProperty, SerializedObject, and PropertyField to fix the CustomEditor script to be as such:

using UnityEditor;

[CustomEditor(typeof(PlayerInputController))]
[CanEditMultipleObjects]
public class PlayerInputControllerEditor : Editor
{

    SerializedProperty playerSelect;

    void OnEnable() {
            playerSelect = serializedObject.FindProperty("player");
    }

    public override void OnInspectorGUI()
    {
        // base.OnInspectorGUI();
        serializedObject.Update();
        PlayerInputController script = target as PlayerInputController;

        EditorGUILayout.PropertyField(playerSelect);

        EditorGUILayout.Space();

        if (script.player == PlayerInputController.Player.Player1) {
            // Show Player1Events
            ShowPlayerEvents("player1MoveInputEvent", "player1JumpEvent");
        
        } else if (script.player == PlayerInputController.Player.Player2) {
            // Show Player2Events
            ShowPlayerEvents("player2MoveInputEvent", "player2JumpEvent");
        }

        serializedObject.ApplyModifiedProperties ();

    }

    void ShowPlayerEvents(string moveEvent, string jumpEvent) {
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty(moveEvent), true);
        EditorGUILayout.PropertyField(this.serializedObject.FindProperty(jumpEvent), true);
    }
}

Here are the links from Unity Answers and the documentation that I used to get this solution: