Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unity Multiplayer Mirror Sync Active state of gameobject

Discussion in 'Multiplayer' started by Zeepblok, Dec 1, 2020.

  1. Zeepblok

    Zeepblok

    Joined:
    Sep 26, 2015
    Posts:
    15
    Hi,

    I have a question. I use mirror. If a players is being clicked I want his model to be deactivated.
    Code (CSharp):
    1. model.SetActive(false);
    The problem is, it works perfectly on the client whos model is being disabled, but all other players still see the model. How do I sync this state to all my players? I now have this code:


    Code (CSharp):
    1.     private void ShootBullet()
    2.     {
    3.         Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    4.         RaycastHit hit;
    5.         shooting = true;
    6.         //audioSource.PlayOneShot(pew);
    7.        
    8.         if (Physics.Raycast(ray, out hit))
    9.         {
    10.             Debug.DrawLine(ray.origin, hit.point,Color.green);
    11.             if (hit.collider.CompareTag("Player"))
    12.             {
    13.                 gameObject.GetComponent<HealthSystem>().CmdMagic(hit.collider.gameObject, 10);
    14.             }
    15.         }
    16.  
    17.     }
    Code (CSharp):
    1. [Command]
    2.     public void CmdMagic(GameObject target, int damage)
    3.     {
    4.         target.GetComponent<HealthSystem>().health -= damage;
    5.         NetworkIdentity opponentIdentity = target.GetComponent<NetworkIdentity>();
    6.         TargetDoMagic(opponentIdentity.connectionToClient, opponentIdentity.netId.ToString(),damage);
    7.     }
    8.  
    9.     //Gebeurd alleen op je target
    10.     [TargetRpc]
    11.     public void TargetDoMagic(NetworkConnection target, string netID,int damage)
    12.     {
    13.         Text health = GameObject.Find("text_healthCounter").GetComponent<Text>();
    14.         GameObject model = GameObject.Find("NetID" + netID);
    15.         Debug.Log("NetID" + netID);
    16.      
    17.         int curHealth = Convert.ToInt32(health.text);
    18.         curHealth -= damage;
    19.         model.transform.Find("playerModel").gameObject.SetActive(false);
    20.  
    21.  
    22.     //not using this yet.
    23.         if (curHealth <= 0)
    24.         {
    25.             //tGo.gameObject.transform.Find("playerModel").gameObject.SetActive(false);
    26.            // model.SetActive(false);
    27.         }
    28.  
    29.         health.text = curHealth.ToString();
    30.     }