Today, I learned about Destroy(gameObject)
Destroy isn’t ran immediately, but at the end of the frame.
I was testing the below code and noticed that the stump and treeTop would spawn twice instead of a single time. Turns out that when my Player hits the Tree via Collider detection, the Collider would hit the gameObject twice before the Destroy takes place.
I read there’s another option of using Destroy Immediate, but absolutely not recommended (many posts on the why’s if you google it)
I had a few quick fixes on the table, but ultimately decided to shorten the Player hit collider’s enabled time from 0.5f to 0.2f.
[Rpc(SendTo.Everyone)]
private void SpawnVisualsClientRpc(float randomYRotation)
{
// Destroy the original tree
Destroy(gameObject);
// Spawn stump
var stump = Instantiate(treeStumpPrefab, transform.position, transform.rotation);
stump.transform.localScale = transform.localScale;
Destroy(stump, 5f);
// Spawn top
var treeTop = Instantiate(treeTopPrefab, transform.position,
Quaternion.Euler(0f, randomYRotation, 0f));
treeTop.transform.localScale = transform.localScale;
Destroy(treeTop, 5f);
}

