Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by eidnog · Sep 08, 2018 at 04:00 PM · serializationcustomeditor

Custom Editor doesn't keep changes made on Inspector

I was trying to create a Custom Editor to help designing a simple battle system, but I stumbled across a problem. The editor works when I'm on Play Mode, but any changes made on Editor Mode doesn't persist on Play Mode. It seems like everything is being serialized correctly, so I really don't have any idea about what could be causing this problem. Here is the code at the moment: Here's my CustomEditor Script:

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(Round))]
 public class RoundEditor : Editor
 {
     Round round;
 
     public void Awake()
     {
         round = target as Round;
     }
 
     public override void OnInspectorGUI()
     {
         base.OnInspectorGUI();
 
         for (int i = 0; i < round.Enemies.Length; i++)
         {
             EditorGUILayout.BeginHorizontal();
 
             EditorGUILayout.LabelField("Enemy " + (i + 1), GUILayout.Width(70));
 
             round.Enemies[i] = (Enemy)EditorGUILayout.ObjectField(round.Enemies[i], typeof(Enemy), true);
 
             if (round.Enemies[i] != null)
                 round.Enemies[i].currentPattern = ToPattern(round.Enemies[i], EditorGUILayout.Popup(ToIndex(round.Enemies[i], round.Enemies[i].currentPattern), round.Enemies[i].PatternOptions));
 
             EditorGUILayout.EndHorizontal();
         }
     }
 
     public int ToIndex(Enemy enemy, Pattern pattern)
     {
         int index = 0;
 
         for (int i = 0; i < enemy.Patterns.Length; i++)
         {
             if (enemy.Patterns[i] == pattern)
             {
                 index = i;
                 break;
             }
         }
 
         return index;
     }
 
     public Pattern ToPattern(Enemy enemy, int index)
     {
         Debug.Log("Converting to... " + index);
         Pattern pattern = enemy.Patterns[index];
         return pattern;
     }
 }


And the other classes that I use:

 [System.Serializable]
 public class Round : MonoBehaviour
 {
     [SerializeField]
     private Enemy[] enemies;    
 
     public Enemy[] Enemies
     {
         get { return enemies; }
         set { enemies = value; }
     }
 }

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Enemy : MonoBehaviour
 {
     #region ATTRIBUTES
     [SerializeField]
     private Pattern[] patterns;
 
     public Pattern currentPattern;
 
     private Animator animator;
     #endregion
 
     #region PROPERTIES
     public Pattern[] Patterns
     { get { return patterns; } }
 
     public string[] PatternOptions
     {
         get
         {
             string[] options = new string[patterns.Length];
 
             for (int i = 0; i < patterns.Length; i++)
                 options[i] = patterns[i].Title;
 
             return options;
         }
     }
     #endregion
 
     #region METHODS
     public void Update()
     {
         currentPattern.Shoot(transform.position, transform.rotation);
     }
 
     public void Attack(Pattern pattern)
     {
         pattern.Shoot(transform.position, transform.rotation);
     }
     #endregion
 }
 

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Pattern
 {
     #region ATTRIBUTES
     [SerializeField]
     private string title = "Pattern";
     [SerializeField]
     private ProjectileData[] projectiles;
 
     private float index = 0;
 
     public string Title
     {
         get
         {
             return title;
         }
 
         set
         {
             title = value;
         }
     }
     #endregion
 
     #region METHODS
     public void Shoot(Vector3 position, Quaternion rotation)
     {
         bool newIndex = false;
 
         foreach (ProjectileData projectile in projectiles)
         {
             if (Time.time > projectile.TimeToFire)
             {
                 projectile.TimeToFire = Time.time + 1 / projectile.FireRate;
 
                 Projectile clone = MonoBehaviour.Instantiate(projectile.Projectile, position, rotation);
                 clone.SetAttributes(projectile.Title, projectile.Speed, projectile.Angle, index);
 
                 newIndex = true;
             }
             else
                 continue;
         }
 
         if (newIndex)
             index++;
     }
     #endregion
 }

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
1

Answer by dan_wipf · Sep 08, 2018 at 06:05 PM

add this line at the end of the editor:

 serializedObject.ApplyModifiedProperties();
Comment
Add comment · Show 2 · 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
avatar image eidnog · Sep 09, 2018 at 02:17 AM 0
Share

Well, this didn't work either, but thanks for the answer!

avatar image dan_wipf · Sep 09, 2018 at 02:26 PM 0
Share

use propertyfield ins$$anonymous$$d of object field

https://docs.unity3d.com/ScriptReference/EditorGUILayout.PropertyField.html

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

92 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

Related Questions

Undo.RecordObject works with GUI.Button but not with GUILayout.Vector3Field 1 Answer

Serializing object in scene? 0 Answers

Invalid iteration - (You need to stop calling Next when it returns false) 2 Answers

How would I go about storing 3D object data into a database? 5 Answers

Storing data about an asset only in the editor 2 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