How can I add to a List in [InitializeOnLoad] script only once?











up vote
0
down vote

favorite












The problem when using InitializeOnLoad it's invalidating all the time.
Then even if I'm making an instance once for a List at the top of the script it will keep making instance for it all the time and adding more and more items to the List.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public static class CustomHierarchy
{
private static Vector2 offset = new Vector2(0, 2);
public static Color gameObjectFontColor = Color.black;
public static Color prefabOrgFontColor = Color.black;
public static Color prefabModFontColor = Color.white;
public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
public static Color meshRendererColor = Color.yellow;

public static List<GameObject> gameobjectsHasAll = new List<GameObject>();

static CustomHierarchy()
{
EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
}
private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
{
Color fontColor = gameObjectFontColor;
Color backgroundColor = new Color(.76f, .76f, .76f);
FontStyle styleFont = FontStyle.Normal;
var obj = EditorUtility.InstanceIDToObject(instanceID);
GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

if (Selection.instanceIDs.Contains(instanceID))
{
backgroundColor = new Color(0.24f, 0.48f, 0.90f);
}
if (obj != null)
{
var prefabType = PrefabUtility.GetPrefabType(obj);
if (gameObj.activeInHierarchy == false)
{
backgroundColor = inActiveColor;
}

if (prefabType == PrefabType.PrefabInstance)
{
styleFont = FontStyle.Bold;
PropertyModification prefabMods = PrefabUtility.GetPropertyModifications(obj);
foreach (PropertyModification prefabMod in prefabMods)
{
if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
if (!gameobjectsHasAll.Contains(gameObj))
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
else
{
fontColor = prefabModFontColor;
}

break;
}
}
if (fontColor != prefabModFontColor)
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
else
{
fontColor = prefabOrgFontColor;
}
}
}
else
{
if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}
}
Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
EditorGUI.DrawRect(selectionRect, backgroundColor);
EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
{
normal = new GUIStyleState() { textColor = fontColor },
fontStyle = styleFont
}
);
}
}

public static bool HasAllComponents(GameObject gameObject, params System.Type types)
{
for (int i = 0; i < types.Length; i++)
{
if (gameObject.GetComponent(types[i]) == null)
return false;
}

return true;
}
}


The List variable is:



public static List<GameObject> gameobjectsHasAll = new List<GameObject>();


And I'm adding objects to the list in 3 places in the code in each place where HasAllComponents is true for example:



if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
{
gameobjectsHasAll.Add(gameObj);
fontColor = meshRendererColor;
}


In the Hierarchy I have 6 items that meet the HasAllComponents condition.
But at the first time the List contain 36 items it keep adding the 6 items 6 times.



Then next 120 items or so and it keep adding the same items nonstop.
I'm calling this List from another script EditorWindow script:



using System;
using UnityEditor;
using UnityEngine;
using System.Collections;
using System.Reflection;
using System.Linq;
using System.Collections.Generic;

public class HierarchyEditor : EditorWindow
{
public static bool multipleComponents = false;
public static string multipleComponentsString = "";

private static SearchableEditorWindow hierarchy { get; set; }
private string filterText = "";
private string oldFilterText = "";

[MenuItem("Tools/Hierarchy Editor")]
public static void ShowWindow()
{
GetWindow<HierarchyEditor>("HierarchyEditor");

Test();
}


Where Test is:



private static void Test()
{
var all = CustomHierarchy.gameobjectsHasAll;
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    The problem when using InitializeOnLoad it's invalidating all the time.
    Then even if I'm making an instance once for a List at the top of the script it will keep making instance for it all the time and adding more and more items to the List.



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using UnityEditor;
    using UnityEngine;

    [InitializeOnLoad]
    public static class CustomHierarchy
    {
    private static Vector2 offset = new Vector2(0, 2);
    public static Color gameObjectFontColor = Color.black;
    public static Color prefabOrgFontColor = Color.black;
    public static Color prefabModFontColor = Color.white;
    public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
    public static Color meshRendererColor = Color.yellow;

    public static List<GameObject> gameobjectsHasAll = new List<GameObject>();

    static CustomHierarchy()
    {
    EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
    }
    private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
    {
    Color fontColor = gameObjectFontColor;
    Color backgroundColor = new Color(.76f, .76f, .76f);
    FontStyle styleFont = FontStyle.Normal;
    var obj = EditorUtility.InstanceIDToObject(instanceID);
    GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

    if (Selection.instanceIDs.Contains(instanceID))
    {
    backgroundColor = new Color(0.24f, 0.48f, 0.90f);
    }
    if (obj != null)
    {
    var prefabType = PrefabUtility.GetPrefabType(obj);
    if (gameObj.activeInHierarchy == false)
    {
    backgroundColor = inActiveColor;
    }

    if (prefabType == PrefabType.PrefabInstance)
    {
    styleFont = FontStyle.Bold;
    PropertyModification prefabMods = PrefabUtility.GetPropertyModifications(obj);
    foreach (PropertyModification prefabMod in prefabMods)
    {
    if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
    {
    if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
    {
    if (!gameobjectsHasAll.Contains(gameObj))
    gameobjectsHasAll.Add(gameObj);
    fontColor = meshRendererColor;
    }
    else
    {
    fontColor = prefabModFontColor;
    }

    break;
    }
    }
    if (fontColor != prefabModFontColor)
    {
    if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
    {
    gameobjectsHasAll.Add(gameObj);
    fontColor = meshRendererColor;
    }
    else
    {
    fontColor = prefabOrgFontColor;
    }
    }
    }
    else
    {
    if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
    {
    gameobjectsHasAll.Add(gameObj);
    fontColor = meshRendererColor;
    }
    }
    Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
    EditorGUI.DrawRect(selectionRect, backgroundColor);
    EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
    {
    normal = new GUIStyleState() { textColor = fontColor },
    fontStyle = styleFont
    }
    );
    }
    }

    public static bool HasAllComponents(GameObject gameObject, params System.Type types)
    {
    for (int i = 0; i < types.Length; i++)
    {
    if (gameObject.GetComponent(types[i]) == null)
    return false;
    }

    return true;
    }
    }


    The List variable is:



    public static List<GameObject> gameobjectsHasAll = new List<GameObject>();


    And I'm adding objects to the list in 3 places in the code in each place where HasAllComponents is true for example:



    if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
    {
    gameobjectsHasAll.Add(gameObj);
    fontColor = meshRendererColor;
    }


    In the Hierarchy I have 6 items that meet the HasAllComponents condition.
    But at the first time the List contain 36 items it keep adding the 6 items 6 times.



    Then next 120 items or so and it keep adding the same items nonstop.
    I'm calling this List from another script EditorWindow script:



    using System;
    using UnityEditor;
    using UnityEngine;
    using System.Collections;
    using System.Reflection;
    using System.Linq;
    using System.Collections.Generic;

    public class HierarchyEditor : EditorWindow
    {
    public static bool multipleComponents = false;
    public static string multipleComponentsString = "";

    private static SearchableEditorWindow hierarchy { get; set; }
    private string filterText = "";
    private string oldFilterText = "";

    [MenuItem("Tools/Hierarchy Editor")]
    public static void ShowWindow()
    {
    GetWindow<HierarchyEditor>("HierarchyEditor");

    Test();
    }


    Where Test is:



    private static void Test()
    {
    var all = CustomHierarchy.gameobjectsHasAll;
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      The problem when using InitializeOnLoad it's invalidating all the time.
      Then even if I'm making an instance once for a List at the top of the script it will keep making instance for it all the time and adding more and more items to the List.



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Reflection;
      using UnityEditor;
      using UnityEngine;

      [InitializeOnLoad]
      public static class CustomHierarchy
      {
      private static Vector2 offset = new Vector2(0, 2);
      public static Color gameObjectFontColor = Color.black;
      public static Color prefabOrgFontColor = Color.black;
      public static Color prefabModFontColor = Color.white;
      public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
      public static Color meshRendererColor = Color.yellow;

      public static List<GameObject> gameobjectsHasAll = new List<GameObject>();

      static CustomHierarchy()
      {
      EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
      }
      private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
      {
      Color fontColor = gameObjectFontColor;
      Color backgroundColor = new Color(.76f, .76f, .76f);
      FontStyle styleFont = FontStyle.Normal;
      var obj = EditorUtility.InstanceIDToObject(instanceID);
      GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

      if (Selection.instanceIDs.Contains(instanceID))
      {
      backgroundColor = new Color(0.24f, 0.48f, 0.90f);
      }
      if (obj != null)
      {
      var prefabType = PrefabUtility.GetPrefabType(obj);
      if (gameObj.activeInHierarchy == false)
      {
      backgroundColor = inActiveColor;
      }

      if (prefabType == PrefabType.PrefabInstance)
      {
      styleFont = FontStyle.Bold;
      PropertyModification prefabMods = PrefabUtility.GetPropertyModifications(obj);
      foreach (PropertyModification prefabMod in prefabMods)
      {
      if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      if (!gameobjectsHasAll.Contains(gameObj))
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      else
      {
      fontColor = prefabModFontColor;
      }

      break;
      }
      }
      if (fontColor != prefabModFontColor)
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      else
      {
      fontColor = prefabOrgFontColor;
      }
      }
      }
      else
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      }
      Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
      EditorGUI.DrawRect(selectionRect, backgroundColor);
      EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
      {
      normal = new GUIStyleState() { textColor = fontColor },
      fontStyle = styleFont
      }
      );
      }
      }

      public static bool HasAllComponents(GameObject gameObject, params System.Type types)
      {
      for (int i = 0; i < types.Length; i++)
      {
      if (gameObject.GetComponent(types[i]) == null)
      return false;
      }

      return true;
      }
      }


      The List variable is:



      public static List<GameObject> gameobjectsHasAll = new List<GameObject>();


      And I'm adding objects to the list in 3 places in the code in each place where HasAllComponents is true for example:



      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }


      In the Hierarchy I have 6 items that meet the HasAllComponents condition.
      But at the first time the List contain 36 items it keep adding the 6 items 6 times.



      Then next 120 items or so and it keep adding the same items nonstop.
      I'm calling this List from another script EditorWindow script:



      using System;
      using UnityEditor;
      using UnityEngine;
      using System.Collections;
      using System.Reflection;
      using System.Linq;
      using System.Collections.Generic;

      public class HierarchyEditor : EditorWindow
      {
      public static bool multipleComponents = false;
      public static string multipleComponentsString = "";

      private static SearchableEditorWindow hierarchy { get; set; }
      private string filterText = "";
      private string oldFilterText = "";

      [MenuItem("Tools/Hierarchy Editor")]
      public static void ShowWindow()
      {
      GetWindow<HierarchyEditor>("HierarchyEditor");

      Test();
      }


      Where Test is:



      private static void Test()
      {
      var all = CustomHierarchy.gameobjectsHasAll;
      }









      share|improve this question













      The problem when using InitializeOnLoad it's invalidating all the time.
      Then even if I'm making an instance once for a List at the top of the script it will keep making instance for it all the time and adding more and more items to the List.



      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Reflection;
      using UnityEditor;
      using UnityEngine;

      [InitializeOnLoad]
      public static class CustomHierarchy
      {
      private static Vector2 offset = new Vector2(0, 2);
      public static Color gameObjectFontColor = Color.black;
      public static Color prefabOrgFontColor = Color.black;
      public static Color prefabModFontColor = Color.white;
      public static Color inActiveColor = new Color(0.01f, 0.4f, 0.25f);
      public static Color meshRendererColor = Color.yellow;

      public static List<GameObject> gameobjectsHasAll = new List<GameObject>();

      static CustomHierarchy()
      {
      EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
      }
      private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
      {
      Color fontColor = gameObjectFontColor;
      Color backgroundColor = new Color(.76f, .76f, .76f);
      FontStyle styleFont = FontStyle.Normal;
      var obj = EditorUtility.InstanceIDToObject(instanceID);
      GameObject gameObj = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

      if (Selection.instanceIDs.Contains(instanceID))
      {
      backgroundColor = new Color(0.24f, 0.48f, 0.90f);
      }
      if (obj != null)
      {
      var prefabType = PrefabUtility.GetPrefabType(obj);
      if (gameObj.activeInHierarchy == false)
      {
      backgroundColor = inActiveColor;
      }

      if (prefabType == PrefabType.PrefabInstance)
      {
      styleFont = FontStyle.Bold;
      PropertyModification prefabMods = PrefabUtility.GetPropertyModifications(obj);
      foreach (PropertyModification prefabMod in prefabMods)
      {
      if (prefabMod.propertyPath.ToString() != "m_Name" && prefabMod.propertyPath.ToString() != "m_LocalPosition.x" && prefabMod.propertyPath.ToString() != "m_LocalPosition.y" && prefabMod.propertyPath.ToString() != "m_LocalPosition.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.x" && prefabMod.propertyPath.ToString() != "m_LocalRotation.y" && prefabMod.propertyPath.ToString() != "m_LocalRotation.z" && prefabMod.propertyPath.ToString() != "m_LocalRotation.w" && prefabMod.propertyPath.ToString() != "m_RootOrder" && prefabMod.propertyPath.ToString() != "m_IsActive")
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      if (!gameobjectsHasAll.Contains(gameObj))
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      else
      {
      fontColor = prefabModFontColor;
      }

      break;
      }
      }
      if (fontColor != prefabModFontColor)
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      else
      {
      fontColor = prefabOrgFontColor;
      }
      }
      }
      else
      {
      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }
      }
      Rect offsetRect = new Rect(selectionRect.position + offset, selectionRect.size);
      EditorGUI.DrawRect(selectionRect, backgroundColor);
      EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle()
      {
      normal = new GUIStyleState() { textColor = fontColor },
      fontStyle = styleFont
      }
      );
      }
      }

      public static bool HasAllComponents(GameObject gameObject, params System.Type types)
      {
      for (int i = 0; i < types.Length; i++)
      {
      if (gameObject.GetComponent(types[i]) == null)
      return false;
      }

      return true;
      }
      }


      The List variable is:



      public static List<GameObject> gameobjectsHasAll = new List<GameObject>();


      And I'm adding objects to the list in 3 places in the code in each place where HasAllComponents is true for example:



      if (HasAllComponents(gameObj, typeof(MeshRenderer), typeof(BoxCollider)))
      {
      gameobjectsHasAll.Add(gameObj);
      fontColor = meshRendererColor;
      }


      In the Hierarchy I have 6 items that meet the HasAllComponents condition.
      But at the first time the List contain 36 items it keep adding the 6 items 6 times.



      Then next 120 items or so and it keep adding the same items nonstop.
      I'm calling this List from another script EditorWindow script:



      using System;
      using UnityEditor;
      using UnityEngine;
      using System.Collections;
      using System.Reflection;
      using System.Linq;
      using System.Collections.Generic;

      public class HierarchyEditor : EditorWindow
      {
      public static bool multipleComponents = false;
      public static string multipleComponentsString = "";

      private static SearchableEditorWindow hierarchy { get; set; }
      private string filterText = "";
      private string oldFilterText = "";

      [MenuItem("Tools/Hierarchy Editor")]
      public static void ShowWindow()
      {
      GetWindow<HierarchyEditor>("HierarchyEditor");

      Test();
      }


      Where Test is:



      private static void Test()
      {
      var all = CustomHierarchy.gameobjectsHasAll;
      }






      c# unity3d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 5:48









      Dubi Duboni

      1118




      1118
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Before all of the lines



          gameobjectsHasAll.Add(gameObj);


          check if the object is already in the list and only add it if it is not there yet



          if(!gameobjectsHasAll.Contains(gameObj))
          {
          gameobjectsHasAll.Add(gameObj);
          fontColor = meshRendererColor;
          }


          so far it seems that you check this only once.






          share|improve this answer






























            up vote
            1
            down vote













            I believe you need to process only one element inside your HandleHierarchyWindowItemOnGUI method as you assign it to EditorApplication.hierarchyWindowItemOnGUI delegate. Manual says "Delegate for OnGUI events for every visible list item in the HierarchyWindow." (https://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowItemOnGUI.html), so for each list element (you have 6) it call it for each element again. And here is unity live cycle (https://docs.unity3d.com/Manual/ExecutionOrder.html), you see it continuously calling OnGUI(), so as you list is static instance of cause it does not remove previous objects from it, and as OnGUI() processing list and create new instances which pass through your check, list will grow and grow.



            So you may want to clear list inside you HandleHierarchyWindowItemOnGUI method, before processing it again.






            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202165%2fhow-can-i-add-to-a-list-in-initializeonload-script-only-once%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Before all of the lines



              gameobjectsHasAll.Add(gameObj);


              check if the object is already in the list and only add it if it is not there yet



              if(!gameobjectsHasAll.Contains(gameObj))
              {
              gameobjectsHasAll.Add(gameObj);
              fontColor = meshRendererColor;
              }


              so far it seems that you check this only once.






              share|improve this answer



























                up vote
                1
                down vote



                accepted










                Before all of the lines



                gameobjectsHasAll.Add(gameObj);


                check if the object is already in the list and only add it if it is not there yet



                if(!gameobjectsHasAll.Contains(gameObj))
                {
                gameobjectsHasAll.Add(gameObj);
                fontColor = meshRendererColor;
                }


                so far it seems that you check this only once.






                share|improve this answer

























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Before all of the lines



                  gameobjectsHasAll.Add(gameObj);


                  check if the object is already in the list and only add it if it is not there yet



                  if(!gameobjectsHasAll.Contains(gameObj))
                  {
                  gameobjectsHasAll.Add(gameObj);
                  fontColor = meshRendererColor;
                  }


                  so far it seems that you check this only once.






                  share|improve this answer














                  Before all of the lines



                  gameobjectsHasAll.Add(gameObj);


                  check if the object is already in the list and only add it if it is not there yet



                  if(!gameobjectsHasAll.Contains(gameObj))
                  {
                  gameobjectsHasAll.Add(gameObj);
                  fontColor = meshRendererColor;
                  }


                  so far it seems that you check this only once.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 at 6:23

























                  answered Nov 8 at 6:15









                  derHugo

                  3,53721026




                  3,53721026
























                      up vote
                      1
                      down vote













                      I believe you need to process only one element inside your HandleHierarchyWindowItemOnGUI method as you assign it to EditorApplication.hierarchyWindowItemOnGUI delegate. Manual says "Delegate for OnGUI events for every visible list item in the HierarchyWindow." (https://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowItemOnGUI.html), so for each list element (you have 6) it call it for each element again. And here is unity live cycle (https://docs.unity3d.com/Manual/ExecutionOrder.html), you see it continuously calling OnGUI(), so as you list is static instance of cause it does not remove previous objects from it, and as OnGUI() processing list and create new instances which pass through your check, list will grow and grow.



                      So you may want to clear list inside you HandleHierarchyWindowItemOnGUI method, before processing it again.






                      share|improve this answer

























                        up vote
                        1
                        down vote













                        I believe you need to process only one element inside your HandleHierarchyWindowItemOnGUI method as you assign it to EditorApplication.hierarchyWindowItemOnGUI delegate. Manual says "Delegate for OnGUI events for every visible list item in the HierarchyWindow." (https://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowItemOnGUI.html), so for each list element (you have 6) it call it for each element again. And here is unity live cycle (https://docs.unity3d.com/Manual/ExecutionOrder.html), you see it continuously calling OnGUI(), so as you list is static instance of cause it does not remove previous objects from it, and as OnGUI() processing list and create new instances which pass through your check, list will grow and grow.



                        So you may want to clear list inside you HandleHierarchyWindowItemOnGUI method, before processing it again.






                        share|improve this answer























                          up vote
                          1
                          down vote










                          up vote
                          1
                          down vote









                          I believe you need to process only one element inside your HandleHierarchyWindowItemOnGUI method as you assign it to EditorApplication.hierarchyWindowItemOnGUI delegate. Manual says "Delegate for OnGUI events for every visible list item in the HierarchyWindow." (https://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowItemOnGUI.html), so for each list element (you have 6) it call it for each element again. And here is unity live cycle (https://docs.unity3d.com/Manual/ExecutionOrder.html), you see it continuously calling OnGUI(), so as you list is static instance of cause it does not remove previous objects from it, and as OnGUI() processing list and create new instances which pass through your check, list will grow and grow.



                          So you may want to clear list inside you HandleHierarchyWindowItemOnGUI method, before processing it again.






                          share|improve this answer












                          I believe you need to process only one element inside your HandleHierarchyWindowItemOnGUI method as you assign it to EditorApplication.hierarchyWindowItemOnGUI delegate. Manual says "Delegate for OnGUI events for every visible list item in the HierarchyWindow." (https://docs.unity3d.com/ScriptReference/EditorApplication-hierarchyWindowItemOnGUI.html), so for each list element (you have 6) it call it for each element again. And here is unity live cycle (https://docs.unity3d.com/Manual/ExecutionOrder.html), you see it continuously calling OnGUI(), so as you list is static instance of cause it does not remove previous objects from it, and as OnGUI() processing list and create new instances which pass through your check, list will grow and grow.



                          So you may want to clear list inside you HandleHierarchyWindowItemOnGUI method, before processing it again.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 8 at 6:16









                          maximelian1986

                          741317




                          741317






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53202165%2fhow-can-i-add-to-a-list-in-initializeonload-script-only-once%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Hercules Kyvelos

                              Tangent Lines Diagram Along Smooth Curve

                              Yusuf al-Mu'taman ibn Hud