question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Destroying an object via despawn leads to a warning message

See original GitHub issue

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

  • OS: Windows 10
  • Unity Version: 2020.3.0f1
  • MLAPI Version: 0.1.0
  • MLAPI Commit: 17bd894dff

Thanks, Carl

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
NoelStephensUnitycommented, Apr 5, 2021

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);
        }
    }
0reactions
NoelStephensUnitycommented, Dec 9, 2021

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Destroying an object via despawn leads to a warning ...
Describe the bug Despawning an object leads to warning message "Trying to destroy object N, but it doesn't seem to exist anymore!
Read more >
Question - Destroy(gameobject) vs Despawn()
So to summarize, the Despawn unsubscribse the NetworkObject from everything and sends a message towards all Clients in that process. And ...
Read more >
Scripting API: Object.Destroy
Description. Removes a GameObject, component or asset. The object obj is destroyed immediately after the current Update loop, or t seconds from now...
Read more >
How to Destroy an object in Unity
Learn how to destroy objects and components in Unity, and what happens behind the scenes when you do.
Read more >
Using mirror, and objects are not despawning/being ...
My assumption is that something you are doing on the local client triggers a function in the object to delete/remove itself but since...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found