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

SpawnManager.InstantiateAndSpawn doesn't Instantiate on Server #2911

Open
AsmPrgmC3 opened this issue May 1, 2024 · 1 comment
Open

SpawnManager.InstantiateAndSpawn doesn't Instantiate on Server #2911

AsmPrgmC3 opened this issue May 1, 2024 · 1 comment

Comments

@AsmPrgmC3
Copy link

Description

SpawnManager.InstantiateAndSpawn doesn't instantiate the prefab when run on a server.

Reproduce Steps

  1. Add a cube to the scene with a Network Object component
  2. Create a prefab from that cube and add it to the Network Prefab list; remove the original from the scene
  3. Add an empty to the scene and add the following component:
using Unity.Netcode;
using UnityEngine;

[RequireComponent(typeof(NetworkObject))]
public class Spawner : NetworkBehaviour
{
    public NetworkObject prefab;

    public override void OnNetworkSpawn()
    {
        if (!IsServer)
        {
            return;
        }

        NetworkManager.SpawnManager.InstantiateAndSpawn(prefab);
        NetworkManager.SpawnManager.InstantiateAndSpawn(prefab);
    }
}
  1. Assign the cube prefab to the Spawner component
  2. Start the project as a server (not host)

Actual Outcome

The cube prefab never gets instantiated, causing:

  • No cubes get added to the scene/hierarchy
  • (Inside the Editor only) The first InstantiateAndSpawn causes the error Transform resides in a Prefab asset and cannot be set to prevent data corruption
  • The second InstatiateAndSpawn causes the error SpawnStateException: Object is already spawned

Expected Outcome

Two cubes spawn.

Environment

  • OS: Windows 10 (22H2)
  • Unity Version: 2022.3.7f1
  • Netcode Version: Release 1.8.1
@AsmPrgmC3 AsmPrgmC3 added stat:awaiting triage Status - Awaiting triage from the Netcode team. type:bug Bug Report labels May 1, 2024
@NoelStephensUnity NoelStephensUnity added priority:high stat:import and removed stat:awaiting triage Status - Awaiting triage from the Netcode team. labels May 4, 2024
@NoelStephensUnity
Copy link
Collaborator

NoelStephensUnity commented May 4, 2024

@AsmPrgmC3
Indeed this does look like a bug and we need to make sure our integration tests includes testing server only.
Unless you are using overrides for your prefabs, you can set forceOverride to true and it should when running as a server:

using Unity.Netcode;
using UnityEngine;

[RequireComponent(typeof(NetworkObject))]
public class Spawner : NetworkBehaviour
{
    public NetworkObject prefab;

    public override void OnNetworkSpawn()
    {
        if (!IsServer)
        {
            return;
        }

        NetworkManager.SpawnManager.InstantiateAndSpawn(prefab, forceOverride: true);
        NetworkManager.SpawnManager.InstantiateAndSpawn(prefab, forceOverride: true);
    }
}

This would be a temporary work around until the next update. The fix for this is pretty simple so I am going to go out on a limb and say the fix will be in the next update (whatever version we end up with that proceeds v1.9,1).

Thank you for the detailed information! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants