Search Unity

Third Party Problem with calling function on NetworkPlayer from Client(Mirror)

Discussion in 'Multiplayer' started by Vidiot_Games, Jul 8, 2021.

  1. Vidiot_Games

    Vidiot_Games

    Joined:
    Aug 12, 2020
    Posts:
    5
    Hey would anyone be able to help me with a mirror/unity issue?

    I am trying to implement a feature that whenever a player vehicle dies, it calls a function on the networkplayer script that reduces the lives. The vehicle with the script that calls the function is not a child of the networkplayer, but has the same identity and such. The problem however, is that it only works on the host, and it doesnt work on the client. I think it has to do with server callbacks and stuff, but I wasnt the one who set those up initially on these functions so idk which one is for what reason. Any help would be awesome! (sorry if this is the wrong place for this, I am new to the forums, and asnwers.unity was not working at all)

    Heres the code on the vehicle, and a projectile code that calls the initial function on the vehicle


    [ServerCallback]
    private void OnTriggerEnter(Collider co)
    {
    if (!co.TryGetComponent<Health>(out var health))return;
    health.DealDamage(damageAmount);
    NetworkServer.Destroy(gameObject);

    /* if (co.TryGetComponent<NetworkIdentity>(out var networkIdentity))
    {
    if (networkIdentity.connectionToClient == connectionToClient) { return; }
    }*/

    }



    [ServerCallback]
    public void DealDamage(int damageAmount)
    {
    currentHealth = Mathf.Max(currentHealth - damageAmount, 0);
    if (currentHealth <= 0 && gameObject.tag == "Building")
    {
    StartCoroutine(Respawn());
    }
    else if (currentHealth <= 0)
    {
    MyPlayer player = NetworkClient.localPlayer.GetComponent<MyPlayer>();
    player.ReduceLives();
    if (player.GetLives() == 0) { return; }
    NetworkIdentity thisObject = GetComponent<NetworkIdentity>();
    FindObjectOfType<VehicleViewer>().TargetEnableVehicleViewer(thisObject.connectionToClient, true);
    StartCoroutine(Respawn());
    }
    }