com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

SpawnManager.InstantiateAndSpawn doesn't Instantiate on Server

Open AsmPrgmC3 opened this issue 1 year ago โ€ข 3 comments

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 avatar May 01 '24 13:05 AsmPrgmC3

@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! ๐Ÿ‘

NoelStephensUnity avatar May 04 '24 02:05 NoelStephensUnity

@AsmPrgmC3 ์‹ค์ œ๋กœ ์ด๊ฒƒ์€ ๋ฒ„๊ทธ์ฒ˜๋Ÿผ ๋ณด์ด๊ณ  ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ์— ํ…Œ์ŠคํŠธ ์„œ๋ฒ„๋งŒ ํฌํ•จ๋˜๋„๋ก ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ”„๋ฆฌํŒน์— ์˜ค๋ฒ„๋ผ์ด๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ, forceOverride๋ฅผ true๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์„œ๋ฒ„๋กœ ์‹คํ–‰ํ•  ๋•Œ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

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);
    }
}

์ด๊ฒƒ์€ ๋‹ค์Œ ์—…๋ฐ์ดํŠธ๊นŒ์ง€ ์ž„์‹œ๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ์— ๋Œ€ํ•œ ํ•ด๊ฒฐ์ฑ…์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฏ€๋กœ, ์ €๋Š” ๋‹ค์Œ ์—…๋ฐ์ดํŠธ( v1.9,1๋กœ ์ง„ํ–‰๋  ๋ฒ„์ „ )์— ํ•ด๊ฒฐ์ฑ…์ด ํฌํ•จ๋  ๊ฒƒ์ด๋ผ๊ณ  ๋‹จ์–ธํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ์ •๋ณด ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ๐Ÿ‘

I tested it, but it doesn't seem to have been fixed yet. Still have same bug. i have to set true for forceOverride parameter when running server.

luke-youngmin-cho avatar Sep 19 '24 01:09 luke-youngmin-cho