Skip to content
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

Unable to parent NetworkObject to a NetworkObject only on client #3100

Open
Conundroy opened this issue Oct 15, 2024 · 6 comments
Open

Unable to parent NetworkObject to a NetworkObject only on client #3100

Conundroy opened this issue Oct 15, 2024 · 6 comments
Labels
Investigating Issue is currently being investigated stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report

Comments

@Conundroy
Copy link

Conundroy commented Oct 15, 2024

Description

Unable to parent NetworkObject to a NetworkObject only on client.

Reproduce Steps

  1. Host spawns NetworkObject (DestroyedWithScene = true) and parent it via script.

Actual Outcome

Host:

  • Scene
    • Parent NetworkObject
      • Child NetworkObject

Client:

  • Scene
    • Parent NetworkObject
    • Child NetworkObject

Expected Outcome

Host:

  • Scene
    • Parent NetworkObject
      • Child NetworkObject

Client:

  • Scene
    • Parent NetworkObject
      • Child NetworkObject

Environment

  • OS: Windows
  • Unity Version: 6000.0.22f1
  • Netcode Version: 2.0.0

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

@Conundroy Conundroy added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report labels Oct 15, 2024
@Conundroy Conundroy changed the title Unable to parent NetworkObject to a NetworkObject Unable to parent NetworkObject to a NetworkObject only on client Oct 15, 2024
@NoelStephensUnity
Copy link
Collaborator

@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:

  • Instantiate and spawn parent
  • Instantiate and spawn child
    • Get the NetworkObject of the child.
    • Invoke NetworkObject.TrySetParent

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.

@NoelStephensUnity NoelStephensUnity added the stat:awaiting response Status - Awaiting response from author. label Oct 18, 2024
@Fobri
Copy link

Fobri commented Oct 20, 2024

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.

@Conundroy
Copy link
Author

@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.

@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented Oct 24, 2024

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.

@Fobri
Copy link

Fobri commented Oct 25, 2024

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.

@NoelStephensUnity NoelStephensUnity added the Investigating Issue is currently being investigated label Oct 28, 2024
@NoelStephensUnity
Copy link
Collaborator

@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.
👍

@NoelStephensUnity NoelStephensUnity removed the stat:awaiting response Status - Awaiting response from author. label Dec 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigating Issue is currently being investigated stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report
Projects
None yet
Development

No branches or pull requests

3 participants