You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9b620fb
Browse filesBrowse the repository at this point in the historyBrowse files
authored
fix: spawn disabled in scene placed and network prefab registration (#4093)
* fix
- Issue where disabled in-scene placed NetworkObjects could not be spawned after starting a session.
- Issue where users are allowed to spawn a NetworkObject with a GlobalObjectIdHash value of 0 (zero).
* update
NetworkAnimator not pointing to an animator within awake now just logs a warning as opposed to an error.
* test
Updating tests based on the fixes applied to this branch.
A large portion is related to some integration tests trying to spawn NetworkObjects with a GlobalObjectIdHash value of zero(0).
* update
Style and providing internal write access to the prefabs list.
* test
Potentially last iteration on fixing/updating integration tests.
* test - fix
Using the fixed version of NetworkPrefabHandlerSpawnAndSynchronizeTests.
Fixing some issues with prefab and handler creation.
* style
Fixing formatting issues.
* style
Fixing some standards formatting related issues.
* style
removing trailing spaces.
* style
Super weird one... but using ? as opposed to an if null check.
* style
Removing commented out code that is no longer needed/used.
* update
Committing suggested changes.
Co-authored-by: Emma <emma.mcmillan@unity3d.com>
* update
Applying changes based on review discussion.
* update
Making adjustments based on review discussion.
* style
Removing field that is no longer valid.
Moving Awake higher in NetworkObject as it has become wedged in the middle of the code.
* update
Adding change log entries.
* update
Inverting logic (oops)
* doc
Updating NetworkObject documentation and in-scene placed documentation.
* Update documentation for the two types of NetworkObjects
Clarified the categories of NetworkObjects and their requirements. Added details for dynamically instantiated and in-scene placed NetworkObjects.
* style
White spaces!
* Apply suggestions from code review
Co-authored-by: Amy Reeve <amy.reeve@unity3d.com>
---------
Co-authored-by: Emma <emma.mcmillan@unity3d.com>
Co-authored-by: Amy Reeve <amy.reeve@unity3d.com>
Copy file name to clipboardExpand all lines: com.unity.netcode.gameobjects/CHANGELOG.md
+6Lines changed: 6 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,8 @@ Additional documentation and release notes are available at [Multiplayer Documen
14
14
### Changed
15
15
16
16
17
+
18
+
17
19
### Deprecated
18
20
19
21
@@ -22,6 +24,10 @@ Additional documentation and release notes are available at [Multiplayer Documen
22
24
23
25
### Fixed
24
26
27
+
- Issue with not being able to spawn initially disabled in-scene placed objects. (#4093)
28
+
- Issue with pre-instantiated network prefab instances being marked as in-scene placed. Now pre-instantiated network prefabs are dynamically spawned. (#4093)
29
+
- Issue where a user could spawn runtime created `NetworkObject` that has a GlobalObjectIdHash of zero. These are not valid instances and will no longer be allowed to spawn. (#4093)
Copy file name to clipboardExpand all lines: com.unity.netcode.gameobjects/Documentation~/basics/scenemanagement/inscene-placed-networkobjects.md
+5-1Lines changed: 5 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -184,9 +184,13 @@ public class MyInSceneNetworkObjectBehaviour : NetworkBehaviour
184
184
> [!NOTE]
185
185
> You only need to enable the NetworkObject on the server-side to be able to respawn it. Netcode for GameObjects only enables a disabled in-scene placed NetworkObject on the client-side if the server-side spawns it. This **does not** apply to dynamically spawned `NetworkObjects`. Refer to [the object pooling page](../../advanced-topics/object-pooling.md) for an example of recycling dynamically spawned NetworkObjects.
186
186
187
+
### Pre-disabled in-scene placed NetworkObjects
188
+
189
+
To initialize an in-scene placed NetworkObject in a disabled state and spawn it later, set its GameObject to inactive in the Editor while the respective scene is open. Once a networked session begins, you can re-enable and spawn it at any time.
190
+
187
191
### Setting an in-scene placed NetworkObject to a despawned state when instantiating
188
192
189
-
Since in-scene placed NetworkObjects are automatically spawned when their respective scene has finished loading during a network session, you might run into the scenario where you want it to start in a despawned state until a certain condition has been met. To do this, you need to add some additional code in the `OnNetworkSpawn` part of your NetworkBehaviour component:
193
+
To programmatically disable an in-scene placed NetworkObject, add some additional code in the `OnNetworkSpawn` part of a NetworkBehaviour component:
The following provides the validity requirements for both types.
29
+
30
+
#### Dynamically instantiated network prefabs
31
+
32
+
Dynamically instantiated network prefabs must:
33
+
34
+
* Be a valid [network prefab](./networkobject.md#network-prefabs) created within the Editor.
35
+
* Be registered in a network prefab list that's assigned to your NetworkManager.
36
+
37
+
#### In-scene placed network prefabs
38
+
39
+
In-scene placed network prefabs must:
40
+
41
+
* Be a valid network prefab instance within a scene.
42
+
* Be a GameObject with a NetworkObject component created within the scene while in the Editor.
43
+
44
+
### What is an invalid NetworkObject?
45
+
46
+
GameObjects that have NetworkObject components added to them during runtime are **not supported** and will result in the NetworkObject's `GlobalObjectIdHash` being zero, which causes synchronization issues. In the event you make this mistake, a warning message will be logged and the NetworkObject won't be spawned.
47
+
22
48
### Component order
23
49
24
50
The order of components on a networked GameObject matters. When adding netcode components to a GameObject, ensure that the NetworkObject component is ordered before any NetworkBehaviour components.
// Trap for users just creating things during runtime where this will be zero.
28
+
if(networkObject.GlobalObjectIdHash==0)
29
+
{
30
+
log.Warning(newContext(LogLevel.Developer,$"{nameof(NetworkObject)}'s GlobalObjectIdHash value is zero! Runtime creating of {nameof(NetworkObject)}s is not supported. Skipping processing.").AddNetworkObject(networkObject));
log.Warning(newContext(LogLevel.Developer,$"{nameof(NetworkObject)}'s SceneOrigin doesn't match current scene being processed! Skipping processing.").AddInfo("SceneOrigin",networkObject.SceneOriginHandle).AddNetworkObject(networkObject));
@@ -36,7 +42,15 @@ public void OnProcessScene(Scene scene, BuildReport report)
36
42
continue;
37
43
}
38
44
45
+
// If already marked, the do nothing.
46
+
if(networkObject.InScenePlaced)
47
+
{
48
+
continue;
49
+
}
50
+
39
51
networkObject.InScenePlaced=true;
52
+
// Will not be true when making a build and the values are serialized.
Debug.LogError($"{nameof(NetworkAnimator)}{name} does not have an {nameof(UnityEngine.Animator)} assigned to it. The {nameof(NetworkAnimator)} will not initialize properly.");
771
+
Debug.LogWarning($"{nameof(NetworkAnimator)}{name} does not have an {nameof(UnityEngine.Animator)} assigned to it. The {nameof(NetworkAnimator)} will not initialize properly.");
NetworkManager.Singleton.Log.Error(newLogging.Context(LogLevel.Normal,$"[{nameof(NetworkPrefabs)}][{nameof(AssignPrefabAtIndex)}] Cannot apply prefab to index {index} when the {nameof(m_Prefabs)} count is only {m_Prefabs.Count}!"));
// This should never happen, but in the case it somehow does log an error and remove the duplicate entry
304
-
Debug.LogError($"{nameof(NetworkPrefab)} ({networkObject.name}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {source}!");
380
+
Debug.LogError($"{nameof(NetworkPrefab)} ({nameOrHashOverride}) has a duplicate {nameof(NetworkObject.GlobalObjectIdHash)} source entry value of: {source}!");
0 commit comments