trophy 0
May 2025 34 posts
Sandbox.Component.OnParentChanged : api/Sandbox.Component/OnParentChanged
trophy 25
Jul 2024 8 posts
Note that if you try to change the position of the GameObject inside this method, it will not work if you are using SetParent with keepWorldPosition: true (the default).
https://github.com/Facepunch/sbox-issues/issues/8724

There is a workaround:
 	protected override void OnParentChanged( GameObject oldParent, GameObject newParent )
	{
		_ = MoveAfterwards();
	}

	// we need to wait so we're outside of OnParentChanged
	async Task MoveAfterwards()
	{
		await Task.Yield();

		// it's possible the owner changed during that time, so tell the current owner of the object to move it.
		MoveIntoPosition();
	}

	[Rpc.Owner]
	public void MoveIntoPosition()
	{
		WorldTransform = GameObject.Parent.WorldTransform;
	}
You may not need the third level of indirection if you're not changing owner at the same time, so you can just put the movement directly after Task.Yield().
people
Log in to reply
You can't reply if you're not logged in. That would be crazy.