Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Third Party Problems using mirror that NetworkServer is not active

Discussion in 'Multiplayer' started by Nxect, Nov 22, 2023.

  1. Nxect

    Nxect

    Joined:
    Jan 16, 2023
    Posts:
    5
    I'm using mirror, and when I use raycast to check whether my bullet hit any player and I want the server to tell that player to use a function decrease his health, but error comes that the server is not active.
    My debug info is NetworkServer.active , is forever false.
    Following are my codes, GunItem detect enemies and use function on the enemy.
    Code (CSharp):
    1.    private void GunItem()
    2.    {
    3.  
    4.        Ray hitRay = new Ray(cam.transform.position, cam.transform.forward);
    5.        RaycastHit hitInfo;
    6.        if (Physics.Raycast(hitRay.origin, hitRay.direction,out hitInfo))
    7.        {
    8.            foreach (var layer in gunLayer)
    9.            {
    10.  
    11.                if (layer == hitInfo.collider.gameObject.layer)
    12.                {
    13.                    Debug.Log("Hit player");
    14.  
    15.                    var netId = hitInfo.collider.gameObject.GetComponent<NetworkIdentity>();
    16.                
    17.                    CmdHitEnemy(netId,hitInfo.collider.gameObject.GetComponent<PlayerHealth>(),damage);
    18.                  
    19.                   return;
    20.                }
    21.            }
    22.        }
    23.    }
    24.  
    25.    [TargetRpc]
    26.    private void CmdHitEnemy(NetworkIdentity id, PlayerHealth targetHealth, float damage)
    27.    {
    28.        Debug.Log("Telling hit target...");
    29.        targetHealth.TakeDamage(damage);
    30.    }
     

    Attached Files: