Skip to content
This repository was archived by the owner on Jul 11, 2025. It is now read-only.
This repository was archived by the owner on Jul 11, 2025. It is now read-only.

Check if gameobjects going to be removed in OnShutdown-Method #381

@ChristianGreiner

Description

@ChristianGreiner

Summary

The engine throws an exception when you remove gameobjects in the OnShutdown-Method.

How to reproduce [Bugs]

Here is my code:

private List<GameObject> enemies = new List<GameObject>();

public void OnInit(InitContext context)
{
    if (context == InitContext.Activate)
    {
        // add some gameobjects and put them into the list.
    }
}

public void OnShutdown(ShutdownContext context)
{
    if (context == ShutdownContext.Saving)
    {
        foreach (var enemy in enemies)
        {
            Scene.Current.RemoveObject(enemy);
        }
    }
}

What's my plan?
I like to keep track of "enemies" and I want to remove them after exiting the sandbox-mode.

Attachments

The Exception:

[Core] Error:   OnSaving() of Scene "Data\Scenes\TestLevel" failed: InvalidOperationException: Collection was modified; enumeration operation may not execute.
CallStack:
   at System.Collections.Generic.HashSet`1.Enumerator.MoveNext()
   at System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
   at Duality.Resources.Scene.OnSaving(String saveAsPath) in c:\projects\duality\Duality\Resources\Scene.cs:line 942
   at Duality.Resource.CheckedOnSaving(String saveAsPath) in c:\projects\duality\Duality\Resource.cs:line 215

Jeah, I know that I try to remove gameobjects when the engine wants to save them.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CoreArea: Duality runtime or launcherHelp WantedContributions especially appreciatedNice2HaveBeneficial, but only very slightly soTaskToDo that's neither a Bug, nor a FeatureUnit TestsGood candidate for adding more tests

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions