Destroying an object via despawn leads to a warning message
See original GitHub issueDescribe 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:
- Created 2 years ago
- Comments:11 (7 by maintainers)
Top GitHub Comments
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:
Hi @sk3p71c, I have upgraded your sample project to work with the new Netcode for GameObjects:
I had to make an adjustment in your scripts to replace the using MLAPI namespace with using Unity.Netcode, and the Spawner.cs:
Running your sample yields no warning messages and the cube spawns and falls on top of the host-player’s object.
Cheers, Noel