Unity-Technologies / com.unity.netcode.gameobjects

Netcode for GameObjects is a high-level netcode SDK that provides networking capabilities to GameObject/MonoBehaviour workflows within Unity and sits on top of underlying transport layer.
MIT License
2.12k stars 430 forks source link

Destroying an object via despawn leads to a warning message #677

Closed sk3p71c closed 2 years ago

sk3p71c commented 3 years ago

Describe the bug Despawning an object leads to warning message "Trying to destroy object N, but it doesn't seem to exist anymore!" Note this happens when running as Host, even if there are no other players connected. Other than the warning message the object appears to be destroyed correctly and propagated to any connected clients.

To Reproduce Create a prefab with box collider set as trigger and add a script with the following code. Note the issue doesn't appear limited to this scenario, it was just the simplest example I could think of.

private void OnTriggerEnter(Collider other)
{
    if (IsServer)
    {
        var obj = this.GetComponent<NetworkObject>();
        obj.Despawn(true); // After this, a warning message will be generated.
    }
}

To spawn the object, create an empty game object and associate the following script. Point "target" at the prefab above.

public NetworkObject target;
void Start()
{
    if (IsServer)
    {
        var o = Instantiate(target, this.transform.position, this.transform.rotation);
        o.Spawn();
    }
}

Play as Host. Walk into the spawned object.

Expected behavior On collision with the spawned object, it should be despawned without logging a warning.

Screenshots N/A

Environment (please complete the following information):

Thanks, Carl

JesseOlmer commented 3 years ago

I can't reproduce this with the provided steps & code (the box is successfully spawned, the OnTriggerEnter code successfully executes, the object is destroyed and removed from the scene, but no console output is observed).

Can you please provide a minimal sample project that reproduces this error?

sk3p71c commented 3 years ago

Thanks Jesse. Example project attached below. I stripped out everything except the scripts I mentioned above. StartHost is now called in the spawning script. If you load the demo scene, you should see a cube fall and hit a spawned sphere, triggering the call to destroy and the issue.

Cheers, Carl

mlapiDespawnIssue.zip

sk3p71c commented 3 years ago

Here are some screenshots of what I see. Before collision: image

After collision: image

JesseOlmer commented 3 years ago

I'm not able to repro with your sample on macOS and Unity 2019.4.19f1, 2020.2.7f1, or 2020.3.1f1. I'll mark this for triage again and we can get it scheduled for someone to look at with a Windows 10 machine in case that's somehow related.

sk3p71c commented 3 years ago

Thanks Jesse. I just tried again with 2020.3.1f1 and still see the issue, so looks like it isn't the Unity version but it could be Windows-related.

will-mearns commented 3 years ago

Noel is going to try and repo this on Windows

NoelStephensUnity commented 3 years ago

Hi Skep71c,

I have replicated this warning message on my side. This is was a known issue and there is currently a temporary fix to prevent this message from being displayed in our development branch. If you wanted to implement this fix locally you could replace the NetworkObject.OnDestroy method with this:

    private void OnDestroy()
    {
        if (NetworkManager.Singleton != null && NetworkSpawnManager.SpawnedObjects.ContainsKey(NetworkObjectId))
        {
            NetworkSpawnManager.OnDestroyObject(NetworkObjectId, false);
        }
    }
sk3p71c commented 3 years ago

Great, thanks for letting me know Noel.

NoelStephensUnity commented 3 years ago

Yeah, it is annoying... that issue should go away come V1.0.0, adding that change to the OnDestroy method should make the spam stop for you.

@will-mearns This was a duplicate bug and is being tracked in MTT-556. (An immediate fix is in place currently but will be further reviewed during our scene object spawning work)

will-mearns commented 3 years ago

Awesome, thanks for updating the info/links

NoelStephensUnity commented 2 years ago

Hi @sk3p71c, I have upgraded your sample project to work with the new Netcode for GameObjects: image

I had to make an adjustment in your scripts to replace the using MLAPI namespace with using Unity.Netcode, and the Spawner.cs:

using Unity.Netcode;

public class Spawner : NetworkBehaviour
{
    public NetworkObject target;
    void Start()
    {
        NetworkManager.Singleton.StartHost();

    }

    public override void OnNetworkDespawn()
    {
        if (IsServer)
        {
            var o = Instantiate(target, this.transform.position, this.transform.rotation);
            o.Spawn();
        }
        base.OnNetworkDespawn();
    }
}

Running your sample yields no warning messages and the cube spawns and falls on top of the host-player's object. image

Cheers, Noel