destroy all game objects in the scene except for the local player in csharp

Here's a C# code snippet to destroy all game objects in the scene except for the local player, assuming you are using Unity's networking system:

main.cs
// Get the local player's network ID
var localPlayerID = NetworkClient.connection.identity.netId;

// Loop through all game objects in the scene
foreach (var gameObject in FindObjectsOfType<GameObject>())
{
    // Check if the game object is not the local player and is not a network manager
    if (gameObject.GetComponent<NetworkIdentity>()?.netId != localPlayerID && !gameObject.GetComponent<NetworkManager>())
    {
        // Destroy the game object
        Destroy(gameObject);
    }
}
492 chars
14 lines

This code first gets the local player's network ID using Unity's NetworkClient class. Then it loops through all game objects in the scene using FindObjectsOfType<GameObject>(). For each game object, it checks if it is not the local player (by comparing network IDs) and not a NetworkManager object (which should not be destroyed). If both conditions are true, the game object is destroyed using Unity's Destroy() function.

gistlibby LogSnag