diff --git a/CHANGELOG.md b/CHANGELOG.md
index 32080ae..63c0e52 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -31,6 +31,7 @@ Additions
Fixes
- Fixed tooltips not being bounded correctly for viewports that don't start at the origin
- Fixed a stack overflow exception when a panel's children have just enough height to cause a scroll bar to appear
+- Fixed AddedToUi and RemovedFromUi being called for freshly added or removed children whose parents are not in a ui system
### MLEM.Data
Improvements
diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs
index 74f85b8..cf630c8 100644
--- a/MLEM.Ui/Elements/Element.cs
+++ b/MLEM.Ui/Elements/Element.cs
@@ -569,7 +569,8 @@ namespace MLEM.Ui.Elements {
index = this.children.Count;
this.children.Insert(index, element);
element.Parent = this;
- element.AndChildren(e => e.AddedToUi(this.System, this.Root));
+ if (this.System != null)
+ element.AndChildren(e => e.AddedToUi(this.System, this.Root));
this.OnChildAdded?.Invoke(this, element);
this.SetSortedChildrenDirty();
element.SetAreaDirty();
@@ -588,7 +589,8 @@ namespace MLEM.Ui.Elements {
// upwards to us if the element is auto-positioned
element.SetAreaDirty();
element.Parent = null;
- element.AndChildren(e => e.RemovedFromUi());
+ if (this.System != null)
+ element.AndChildren(e => e.RemovedFromUi());
this.OnChildRemoved?.Invoke(this, element);
this.SetSortedChildrenDirty();
}
@@ -1256,7 +1258,7 @@ namespace MLEM.Ui.Elements {
///
/// Called when this element is added to a and, optionally, a given .
- /// This method is called in and .
+ /// This method is called in for a parent whose is set, as well as .
///
/// The ui system to add to.
/// The root element to add to.
@@ -1269,7 +1271,7 @@ namespace MLEM.Ui.Elements {
///
/// Called when this element is removed from a and .
- /// This method is called in and .
+ /// This method is called in for a parent whose is set, as well as .
///
protected internal virtual void RemovedFromUi() {
var root = this.Root;