Skip to content

fix: Add null check in TrySetParent #2625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions com.unity.netcode.gameobjects/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Additional documentation and release notes are available at [Multiplayer Documen

### Fixed

- Fixed a crash when calling TrySetParent with a null Transform (#2625)
- Fixed issue where a `NetworkTransform` using full precision state updates was losing transform state updates when interpolation was enabled. (#2624)
- Fixed issue where `NetworkObject.SpawnWithObservers` was not being honored for late joining clients. (#2623)
- Fixed issue where invoking `NetworkManager.Shutdown` multiple times, depending upon the timing, could cause an exception. (#2622)
Expand Down
6 changes: 6 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,12 @@ internal void SetNetworkParenting(ulong? latestParent, bool worldPositionStays)
/// <returns>Whether or not reparenting was successful.</returns>
public bool TrySetParent(Transform parent, bool worldPositionStays = true)
{
// If we are removing ourself from a parent
if (parent == null)
{
return TrySetParent((NetworkObject)null, worldPositionStays);
}

var networkObject = parent.GetComponent<NetworkObject>();

// If the parent doesn't have a NetworkObjet then return false, otherwise continue trying to parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,49 @@ public IEnumerator SetParentTryAPI()
Assert.That(m_Cube_NetObjs[setIndex + 1].parent, Is.EqualTo(m_Dude_LeftArm_NetObjs[setIndex + 1]));
Assert.That(m_Cube_NetBhvs[setIndex + 1].ParentNetworkObject, Is.EqualTo(m_Dude_LeftArm_NetObjs[setIndex + 1].GetComponent<NetworkObject>()));
}

Transform nullTransform = null;
GameObject nullGameObject = null;
NetworkObject nullNetworkObject = null;


Assert.That(m_Cube_NetObjs[0].GetComponent<NetworkObject>().TrySetParent(nullTransform));
Assert.That(m_Cube_NetBhvs[0].ParentNetworkObject, Is.EqualTo(null));

nextFrameNumber = Time.frameCount + 2;
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);

for (int setIndex = 0; setIndex < k_ClientInstanceCount; setIndex++)
{
Assert.That(m_Cube_NetObjs[setIndex + 1].parent, Is.EqualTo(null));
Assert.That(m_Cube_NetBhvs[setIndex + 1].ParentNetworkObject, Is.EqualTo(null));
}


Assert.That(m_Cube_NetObjs[0].GetComponent<NetworkObject>().TrySetParent(nullGameObject));
Assert.That(m_Cube_NetBhvs[0].ParentNetworkObject, Is.EqualTo(null));

nextFrameNumber = Time.frameCount + 2;
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);

for (int setIndex = 0; setIndex < k_ClientInstanceCount; setIndex++)
{
Assert.That(m_Cube_NetObjs[setIndex + 1].parent, Is.EqualTo(null));
Assert.That(m_Cube_NetBhvs[setIndex + 1].ParentNetworkObject, Is.EqualTo(null));
}


Assert.That(m_Cube_NetObjs[0].GetComponent<NetworkObject>().TrySetParent(nullNetworkObject));
Assert.That(m_Cube_NetBhvs[0].ParentNetworkObject, Is.EqualTo(null));

nextFrameNumber = Time.frameCount + 2;
yield return new WaitUntil(() => Time.frameCount >= nextFrameNumber);

for (int setIndex = 0; setIndex < k_ClientInstanceCount; setIndex++)
{
Assert.That(m_Cube_NetObjs[setIndex + 1].parent, Is.EqualTo(null));
Assert.That(m_Cube_NetBhvs[setIndex + 1].ParentNetworkObject, Is.EqualTo(null));
}
}
}
}