0

I having problems with unity mirror. When respawn IEnumerator is called, the player respawn but it disconnect from the server, he cant move (only locally) and he cant do commands. this is the respawn code:

using UnityEngine;
using Mirror;
using System.Collections;


public class LaserGun : NetworkBehaviour
{
    public Transform laserTransform;
    public LineRenderer line;

    // Update is called once per frame
    void Update()
    {
        if(isLocalPlayer && Input.GetKeyDown(KeyCode.Q)){
            CmdShoot();
        }
    }

    [Command]
    public void CmdShoot(){
        Ray ray = new Ray(laserTransform.position, laserTransform.forward);
        if(Physics.Raycast(ray, out RaycastHit hit, 100f)){
            print(hit.collider.gameObject);
            if(hit.collider.gameObject.tag == "Player"){
                print("player checked as a player");
                StartCoroutine(Respawn(hit.collider.gameObject));
            }
            print(hit.collider.tag);
            RpcDrawLaser(laserTransform.position, hit.point);
        }
        else{
            RpcDrawLaser(laserTransform.position, laserTransform.position + laserTransform.forward * 100f);
        }
    }

    [Server]
    IEnumerator Respawn(GameObject go){
        print("Player ded");
        NetworkServer.UnSpawn(go);
        yield return new WaitForSeconds(0.5f);
        NetworkServer.Spawn(go);
    }

    [ClientRpc]
    void RpcDrawLaser(Vector3 start, Vector3 end){
        StartCoroutine(LaserFlash(start, end));
    }

    IEnumerator LaserFlash(Vector3 start, Vector3 end){
        line.SetPosition(0, start);
        line.SetPosition(1, end);
        yield return new WaitForSeconds(0.3f);
        line.SetPosition(0, Vector3.zero);
        line.SetPosition(1, Vector3.zero);
    }
}

Any solutions to that problem please?

1 Answer 1

0

I would need the whole script to make sure I am right, but I am pretty sure, that the problem is that you call [Server] via a client (which isn't allowed).

[Server] is used to make sure that only the server can call the function. Not a client, that would explain why the player is disconnected, when the function is called.

It would probably work if you change [Server] to [Command(requiresAuthority = false)]. Link to the [Command] documentation: Mirror documentation If not please send the whole script.

With regards,

Aaron

1
  • Dont work, here have the old code (the actual is corrupted for some reason)
    – Duviz 2
    Commented Sep 21, 2022 at 18:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.