Search Unity

Question Unable to destroy gameObjects for clients

Discussion in 'Multiplayer' started by DylanPotts, Apr 17, 2022.

  1. DylanPotts

    DylanPotts

    Joined:
    May 13, 2021
    Posts:
    3
    I've gotten into mirror somewhat recently and have struggled most with getting NetworkServer.Destroy() to function within my game, currently the specified objects I'm trying to destroy technically get destroyed on the server as once the code runs any joined clients can pass straight through the object that has been destroyed, however it doesn't leave the client's screen and is visible to them. Would greatly appreciate advice with my code, unsure if it's an issue to do with how I've spawned said gameObject or if I'm executing the code to destroy gameObjects incorrectly. Thanks in advance

    IcebergMelting.cs


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5.  
    6. public class IcebergMelting : NetworkBehaviour
    7. {
    8.     public List<GameObject> icebergSections = new List<GameObject>();
    9.  
    10.     public float meltTimer;
    11.     public void Start()
    12.     {
    13.         if (!isLocalPlayer) return;
    14.        StartCoroutine(IcebergMeltEnum());
    15.  
    16.     }
    17.  
    18.     public IEnumerator IcebergMeltEnum()
    19.     {
    20.        WaitForSeconds wait = new WaitForSeconds(meltTimer);
    21.        
    22.         while (true)
    23.         {
    24.             yield return wait;
    25.  
    26.             if (icebergSections.Capacity <= 0)
    27.                 yield return wait;
    28.  
    29.             // gets a section of the iceberg randomly
    30.             GameObject choosenIcebergSection = icebergSections[Random.Range(0, icebergSections.Capacity)];
    31.             // removes it from scene
    32.             ServerDestroy(choosenIcebergSection);
    33.             Debug.Log("this should be destroying wth");
    34.             // removes gameobject from the list
    35.             icebergSections.Remove(choosenIcebergSection);
    36.             // removes empty element from list
    37.             if (icebergSections.Capacity > 0)
    38.                 icebergSections.Capacity -= 1;
    39.         }
    40.     }
    41.  
    42.     [Server]
    43.     public void ServerDestroy(GameObject obj)
    44.     {
    45.         NetworkServer.Destroy(obj);
    46.     }
    47. }