When it happen I need to force to shut down the editor using the task manager.
This is the script I’m using :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[ExecuteAlways]
public class CompareObjects : MonoBehaviour
{
public GameObject mainGame;
public string comparisonObjects;
public float waitTime;
public List<GameObject> allobjects = new List<GameObject>();
public bool startComparingAtStart = false;
private Coroutine comparer;
private void Start()
{
if (Application.isPlaying == false)
{
allobjects = new List<GameObject>();
}
else
{
allobjects = FindObjectsOfType<GameObject>().ToList();
}
if (startComparingAtStart == true)
{
StartComparing();
}
}
public void StartComparing()
{
mainGame.SetActive(false);
if (comparer == null)
{
comparer = StartCoroutine(Compare());
}
}
public void StopComparing()
{
if (comparer != null)
{
comparisonObjects = "";
allobjects = new List<GameObject>();
StopCoroutine(comparer);
mainGame.SetActive(true);
comparer = null;
}
}
IEnumerator Compare()
{
while (true)
{
foreach (GameObject go in allobjects)
{
if (go.name != "Game Manager")
{
comparisonObjects = go.name + " >>>>> " + go.scene.name + " >>>>> is active object";
}
yield return new WaitForSeconds(waitTime);
}
}
}
}
I’m using also [ExecuteAlways] and if (Application.isPlaying == false) since I want that when I quit the game and even if the Coroutine is in the middle if it’s in the middle of the loop to reset the list alObjects to length 0. If not doing it when I quit the game the list is empty but it’s length keep be over 5000 items.
And I want like in the StopComparing if quit the game to reset init the list to length 0.
But the way it is now for some reason it’s making the whole editor freezing and all I can do is forcing it to shut down via task manager.
I also created a editor script for it to make buttons in the inspector :
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(CompareObjects))]
public class CompareObjectsButton : Editor
{
private CompareObjects compareObjects;
private void OnEnable()
{
compareObjects = (CompareObjects)target;
}
public override void OnInspectorGUI()
{
DrawDefaultInspector();
CompareObjects myTarget = (CompareObjects)target;
if (GUILayout.Button("Compare Objects"))
{
myTarget.StartComparing();
}
if (GUILayout.Button("Stop"))
{
myTarget.StopComparing();
}
}
}