Open Conundroy opened 1 month ago
@Conundroy If you could post the associated script it would help me provide better guidance on this issue. Make sure you have spawned both NetworkObject instances before you attempt to parent them. Otherwise, if you are parenting them prior to spawning then that will not synchronize properly.
Order of Operations:
Let me know if this resolves your issue or if not then if you could provide me with a replication project or the script that you are using to instantiate, spawn, and parent the two objects it would be helpful.
I have the same issue, it started appearing when I upgraded from 1.8 to 2.0. The scenario I have is very simple, a networkbehaviour instantiates and spawns an instance of a networkobject, and then parents it under itself called in OnNetworkSpawn. Simplified pseudocode of what I'm doing below.
override void OnNetworkSpawn()
{
var instance = Instantiate(prefab);
instance.GetComponent<NetworkObject>().Spawn();
instance.transform.parent = transform;
}
The object gets correctly parented on server and also late joining clients, but for clients currently in the game the parenting never happens.
@NoelStephensUnity
Here's the script on the parent object/game manager:
public override void OnNetworkSpawn()
{
if (IsHost) GenerateItems();
base.OnNetworkSpawn();
{
void GenerateItems()
{
Mesh mesh = Map.Instance.GetComponentInChildren<MeshFilter>().sharedMesh;
Bounds bounds = mesh.bounds;
// Spawn coins
for (int i = 0; i < numCoins; i++)
{
TrySpawnObject(coin, mesh);
}
}
void TrySpawnObject(GameObject objectPrefab, Mesh mesh)
{
int randomTriangle = Random.Range(0, mesh.triangles.Length / 3) * 3; // Choose a random triangle
Vector3 v0 = mesh.vertices[mesh.triangles[randomTriangle]];
Vector3 v1 = mesh.vertices[mesh.triangles[randomTriangle + 1]];
Vector3 v2 = mesh.vertices[mesh.triangles[randomTriangle + 2]];
// Compute the center of the triangle (polygon face)
Vector3 center = (v0 + v1 + v2) / 3f;
// Get the normal of the triangle
Vector3 normal = Vector3.Cross(v1 - v0, v2 - v0).normalized;
// Apply random offset in the x and z directions within the range (-3, 3)
Vector3 randomOffset = new Vector3(
Random.Range(-3, 3),
0f, // No offset in the Y direction
Random.Range(-3, 3)
);
Vector3 spawnPosition = center + randomOffset;
// Directly instantiate at the calculated position
GameObject newItem = Instantiate(objectPrefab, spawnPosition, Quaternion.identity);
newItem.GetComponent<NetworkObject>().Spawn(true);
newItem.GetComponent<NetworkObject>().TrySetParent(transform);
}
This works as intended in v1.11.0.
Hmmm...generally speaking...instantiating and spawning in the middle of spawning is not recommended as it can lead to issues down the road.
Just out of curiosity, have you tried changing the OnNetworkSpawn
to OnNetworkPostSpawn
?
This would assure all components had finished running through their spawn process prior to spawning more things and parenting them under the NetworkObject
that just finished spawning. There are some updates in v2.0.0 that leverage from NetworkPreSpawn and NetworkPostSpawn ordering which could have impacted spawing in the middle of spawning.
This is the first time I'm hearing that its not recommended to spawn things inside OnNetworkSpawn. How are you supposed to spawn things on server when it initializes then? My entire server logic gets kicked off in a OnNetworkSpawn that loads a map from disk, and populates it by instantiating and spawning objects. So far I haven't had any issue with that. Perhaps because it's an in-scene placed object?
I did also try OnNetworkPostSpawn for this specific issue, and it didn't change anything.
@Fobri I will take a look to see if there is a bug here, but the issue isn't that you can't spawn things during OnNetworkSpawn or OnNetworkPostSpawn but that if during spawning something you spawn another NetworkObject that will be parented under the NetworkObject currently being spawned. However, I will investigate this issue further to see if I can replicate it on my end too and if there was a recent update that is the cause of this issue. 👍
Description
Unable to parent NetworkObject to a NetworkObject only on client.
Reproduce Steps
Actual Outcome
Host:
Client:
Expected Outcome
Host:
Client:
Environment
Additional Context
Works if I spawn and parent in editor, probably has something to do with DesroyedWithScene set to true?
Update: Works when I downgrade to v1.11.0