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 /
avatar image
0
Question by CiberX15 · Mar 25, 2021 at 08:53 PM · editorlisteditor-scripting

How to correctly modify lists in editor? (changes to lists not changed or corrupted)

I am trying to build an editor for placing roads where the editor will record the position of each road on a mono behavior script. Then when a new road is placed, it uses that information to know what kind of road to place (straight, dead-end, T intersection, 4-way intersection, turn). It will also update the existing roads to account for the newly placed road.

The problem I am running into is unity seems to have some major hang-ups on updating lists in editor mode, and I can't seem to get around them. I boiled it down to the simplest script I could think of to have a good example. See the following:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 
 public class Debugafier : MonoBehaviour
 {
     public List<float> ExampleList = new List<float>();
 }
 
 #if UNITY_EDITOR
 public class DebugWindow : EditorWindow
 {
     private static DebugWindow m_ActiveWindow = null;
   
 
     [MenuItem("Tools/Debug...")]
     static void Init()
     {
         // Get existing open window or if none, make a new one:
         if (m_ActiveWindow != null)
         {
             m_ActiveWindow.Close();
         }
 
         m_ActiveWindow = (DebugWindow)EditorWindow.GetWindow(typeof(DebugWindow));
     }
 
     void OnGUI()
     {
         EditorGUILayout.BeginVertical();
         if (GUILayout.Button("Add 2 remove 1"))
         {
             Debugafier debugafier = GameObject.Find("Debugafier").GetComponent<Debugafier>();
 
             EditorUtility.SetDirty(debugafier);
             debugafier.ExampleList.Add(debugafier.ExampleList.Count);
             debugafier.ExampleList.Add(debugafier.ExampleList.Count);
             debugafier.ExampleList.Remove(1);
         }
 
         if (GUILayout.Button("Remove 1"))
         {
             Debugafier debugafier = GameObject.Find("Debugafier").GetComponent<Debugafier>();
 
             EditorUtility.SetDirty(debugafier);
             debugafier.ExampleList.RemoveAt(1);
         }
 
         if (GUILayout.Button("Create New List 0"))
         {
             Debugafier debugafier = GameObject.Find("Debugafier").GetComponent<Debugafier>();
 
             EditorUtility.SetDirty(debugafier);
             debugafier.ExampleList = new List<float>();
         }
 
         if (GUILayout.Button("Create New List 10"))
         {
             Debugafier debugafier = GameObject.Find("Debugafier").GetComponent<Debugafier>();
 
             EditorUtility.SetDirty(debugafier);
             debugafier.ExampleList = new List<float>(10);
         }
 
         if (GUILayout.Button("Create Pre Filled List"))
         {
             Debugafier debugafier = GameObject.Find("Debugafier").GetComponent<Debugafier>();
 
             EditorUtility.SetDirty(debugafier);
             debugafier.ExampleList = new List<float>() {1,2,3,4,5};
         }
         EditorGUILayout.EndVertical();
     }
 }
 #endif
 
 


So based on what is written there, the expected behavior is when the user opens up the Debug Window and clicks the Add 2 Remove 1 button, it adds 2 items to the list, then removes the item in slot 1, with an end result of the list growing by 1.

In reality, this works, twice, before going batshit insane. Starting with a list Click once and you get a list of {0}. Click again and you get a list of {0, 2}. So far so good. But here's where it gets strange. Click a third time and you get a list of {0,2,2,3}. It just... completely skips removing the item. And from that point continues to always go up by 2 and constantly fails to remove the item at index 1, in spite of logging no errors indicating a problem.

I even tried breaking on the problem area and I just watch it gracefully step over the RemoveAt() command without actually removing the item.

The "Remove" button seems to work fine, as do the "Create new List 0" and "Create Pre Filled List" However the "Create New List 10" command, which I would expect to populate the example list slot with a list with a size of 10, instead just creates an empty list again.

I also tried using SetDirty() below the change as well as above it and that does not appear to have any effect. Any help explaining why this is happening and more importantly how to get around it would be greatly appreciated.

Comment
Add comment · Show 2
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 logicandchaos · Mar 27, 2021 at 01:47 PM 0
Share

instead of making an editor script you could try utilizing OnValidate() it is called whenever a value is changed and works in editor as well. Even works on scriptableObjects.

avatar image CiberX15 logicandchaos · Mar 31, 2021 at 03:08 AM 0
Share

I don't think that's applicable in this situation. I have used OnValidate() before and it has proven helpful but unless I am mistaken that runs on the script when a value is changed.

The problem here is that when I change the value (the contents and length of the list) it does not update correctly. So anything OnValidate() could do would be after the change already failed.

0 Replies

· Add your reply
  • Sort: 

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

165 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

Related Questions

How to add a reorderable list on CUSTOM EDITOR WINDOW? 0 Answers

How to prevent Static List from resetting when i run the game in the editor? 1 Answer

Add trasform to list using editor gui. 1 Answer

A node in a childnode? 1 Answer

CustomEditor for a list of polymorphic items 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