The script is EditorWindow type.
private void OnGUI() { var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); } } When I select with the mouse a object in the Editor Hierarchy it will keep making the loop nonstop since selections.Count is large then 0.
I tried to add after the loop:
selections = new List<GameObject>(); But it didn't help.
I want that it will make the loop and the whole code inside the if (selections.Count > 0) only once each time when selecting one or more objects in the editor.
If I select one object make the whole code once if I then selected another more object make the whole code over again for the two selected objects and so on but make it once. Now it will keep making the loop over and over again even if I didn't select more other objects.
Since in the editor the first object is still selected it will keep making the code the loop over and over again.
This is what I tried:
var selections = Selection.objects.OfType<GameObject>().ToList(); if (selections.Count > 0 && isSelected == false) { for (var i = selections.Count - 1; i >= 0; --i) { var selected = selections[i]; transformSelection.Add(selected.transform); } TransformSaver.SaveTransform(transformSelection.ToArray()); tempTransformSelection = transformSelection; transformSelection = new List<Transform>(); isSelected = true; } The flag boolean isSelected is global variable and set to false as init.