Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by MustachedNinja · Aug 03, 2021 at 06:46 PM · custom editorenumpopup

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.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MustachedNinja · Aug 05, 2021 at 04:12 PM

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:

https://docs.unity3d.com/ScriptReference/Editor.html https://docs.unity3d.com/ScriptReference/SerializedProperty.html https://answers.unity.com/questions/231830/serializedproperty-for-enum-with-enumpopup.html

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

167 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

Related Questions

Is it possible to use separator / divider when displaying enum field in inspector? 1 Answer

How To Draw UnityEvent in the Custom Editor? 0 Answers

Creating the audio source graph in custom editor 0 Answers

How do you hide a slider using Custom editor? 0 Answers

error CS0246: The type or namespace name 'MonoScript' could not be found (are you missing a using directive or an assembly reference?) 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