Skip to content

Commit d949e99

Browse files
fix: Endless warning dialogs when NetworkBehaviour is added to NetworkManager - MTT-3252 (#1947)
* fix MTT-3252 This fixes the endless dialog loop issue. * style updating comments and the dialog text copy. * Update CHANGELOG.md Co-authored-by: Unity Netcode CI <[email protected]>
1 parent 9a8cb26 commit d949e99

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
Additional documentation and release notes are available at [Multiplayer Documentation](https://docs-multiplayer.unity3d.com).
8+
### Added
9+
10+
### Changed
11+
12+
### Removed
813

914
### Fixed
1015

1116
- Fixed: Hosting again after failing to host now works correctly
17+
- Fixed endless dialog boxes when adding a NetworkBehaviour to a NetworkManager or vice-versa. (#1947)
1218

1319
- Fixed NetworkManager to cleanup connected client lists after stopping (#1945)
1420
- Fixed: NetworkHide followed by NetworkShow on the same frame works correctly (#1940)

com.unity.netcode.gameobjects/Editor/NetworkBehaviourEditor.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,39 @@ public static void CheckForNetworkObject(GameObject gameObject, bool networkObje
254254

255255
// Now get the root parent transform to the current GameObject (or itself)
256256
var rootTransform = GetRootParentTransform(gameObject.transform);
257+
var networkManager = rootTransform.GetComponent<NetworkManager>();
258+
if (networkManager == null)
259+
{
260+
networkManager = rootTransform.GetComponentInChildren<NetworkManager>();
261+
}
262+
263+
// If there is a NetworkManager, then notify the user that a NetworkManager cannot have NetworkBehaviour components
264+
if (networkManager != null)
265+
{
266+
var networkBehaviours = networkManager.gameObject.GetComponents<NetworkBehaviour>();
267+
var networkBehavioursChildren = networkManager.gameObject.GetComponentsInChildren<NetworkBehaviour>();
268+
if (networkBehaviours.Length > 0 || networkBehavioursChildren.Length > 0)
269+
{
270+
if (EditorUtility.DisplayDialog("NetworkBehaviour or NetworkManager Cannot Be Added", $"{nameof(NetworkManager)}s cannot have {nameof(NetworkBehaviour)} components added to the root parent or any of its children." +
271+
$" Would you like to remove the NetworkManager or NetworkBehaviour?", "NetworkManager", "NetworkBehaviour"))
272+
{
273+
DestroyImmediate(networkManager);
274+
}
275+
else
276+
{
277+
foreach (var networkBehaviour in networkBehaviours)
278+
{
279+
DestroyImmediate(networkBehaviour);
280+
}
281+
282+
foreach (var networkBehaviour in networkBehaviours)
283+
{
284+
DestroyImmediate(networkBehaviour);
285+
}
286+
}
287+
return;
288+
}
289+
}
257290

258291
// Otherwise, check to see if there is any NetworkObject from the root GameObject down to all children.
259292
// If not, notify the user that NetworkBehaviours require that the relative GameObject has a NetworkObject component.

0 commit comments

Comments
 (0)