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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

UNET problem with syncing the change of color.

Discussion in 'UNet' started by anueves1, Jul 14, 2015.

  1. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    I have a door wich changes its color to blue when i press the f key.
    The code for the input and the network syncing of the door are both on the player object and are ase follows:
    Code (CSharp):
    1. private void DoorController()
    2.     {
    3.         /*--------RAY STUFF--------*/
    4.         RaycastHit myHit;
    5.         Ray ray = GameObject.FindGameObjectWithTag("FPSCamera").GetComponent<Camera>().ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2));
    6.        
    7.         CastRay (ray, out myHit, m_RaycastOptions.DoorRayDistance);//Cast a ray.
    8.        
    9.         if (myHit.transform && myHit.transform.GetComponent<TagSystem> ()) {
    10.             m_RaycastOptions.Pointer.enabled = true;
    11.             if (myHit.transform.GetComponent<TagSystem> ().Tag == "Door_Normal") {//Check if we are actually looking at a door.
    12.                 //Execute door code in here.
    13.                 m_RaycastOptions.Pointer.enabled = true;
    14.                 DoorAnimator = myHit.transform.GetComponent<Animator> ();
    15.                
    16.                 bool Opened = DoorAnimator.GetBool ("Opened");
    17.                
    18.                 m_RaycastOptions.Pointer.enabled = true;
    19.  
    20.                 if (Input.GetKeyDown (KeyCode.F)) {
    21.                     if (Opened) {
    22.                         //Close Door.
    23.                         UnlockSys.GetDoorState (false,DoorAnimator);
    24.                         //Play closing sound.
    25.                     } else if (!Opened) {
    26.                         //Open Door.
    27.                         UnlockSys.GetDoorState (true,DoorAnimator);
    28.                         //Play opening sound.
    29.                     }
    30.                 }
    31. }
    And then i have the class wich syncs that color change :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. /// <summary>
    6. /// Class used for the networking of the door animator and door functions.
    7. /// </summary>
    8. public class NetworkUnlockSystem : NetworkBehaviour
    9. {
    10.     public Animator DoorAnimator;
    11.     [SyncVar]public bool DoorOpened;
    12.    
    13.  
    14.      /// <summary>
    15.      /// Used for getting the information that we need for each door.
    16.      /// </summary>
    17.      /// <param name="Opened"></param>
    18.      /// <param name="GotAnim"></param>
    19.      public void GetDoorState(bool Opened,Animator GotAnim)
    20.      {
    21.         if(isLocalPlayer)
    22.         {
    23.             DoorAnimator = GotAnim;
    24.  
    25.             if (isServer)
    26.             {
    27.                 RpcSendDoorStateToAll(Opened, DoorAnimator.transform.name);
    28.                 Debug.Log("Im server");
    29.             }
    30.             else
    31.             {
    32.                 CmdSendDoorStateToServer(Opened, DoorAnimator.transform.name);
    33.                 Debug.Log("Im client");
    34.             }
    35.         }
    36.     }
    37.     [ClientRpc]
    38.     private void RpcSendDoorStateToAll(bool DoorState,string DoorName)
    39.     {
    40.         //Updates the DoorOpened variable on all clients from the server.
    41.         DoorOpened = DoorState;
    42.  
    43.         GameObject.Find(DoorName).GetComponent<MeshRenderer>().material.color = Color.blue;
    44.         /*SINC THE STUFF IN THE CLIENTS*/
    45.         //Works if host does the color change but not if a client does it.
    46.     }
    47.     [Command]
    48.     private void CmdSendDoorStateToServer(bool DoorState,string DoorName)
    49.     {
    50.         //Update the server from a client.
    51.         RpcSendDoorStateToAll(DoorState, DoorAnimator.transform.name);
    52.     }
    53.  
    54. }
    55.  
    Now, this is only working on the host side and by that i mean if the host goes to the door and presses f the color changes to blue for all the clients and for the server but if a client goes to the door and presses f nothing happens, why is this happening?
     
  2. SuperNeon

    SuperNeon

    Joined:
    Mar 16, 2014
    Posts:
    85
    Hi !

    Try to call directly CmdSendDoorStateToServer, I guess isLocalPlayer is false when you are a remote client.

    You should use the same function for a local client and remote client to have the same behaviour or you will have some issue like this one.

    Cheers !
     
  3. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Hi, thanks for the answer!
    But unfortunately that seems to not be the problem.
    I tried removing it and the result is the same :(
     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
  5. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    And how would i look up the door without using GameObject.Find()? Since the most possible thing that could happen is for the players to have different doorAnimators assigned.

    Anyway, also wanted to say that i edited the code and now looks like this:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. /// <summary>
    6. /// Class used for the networking of the door animator and door functions.
    7. /// </summary>
    8. public class NetworkUnlockSystem : NetworkBehaviour
    9. {
    10.     public Animator DoorAnimator;
    11.     [SyncVar]public bool DoorOpened;
    12.    
    13.  
    14.      /// <summary>
    15.      /// Used for getting the information that we need for each door.
    16.      /// </summary>
    17.      /// <param name="Opened"></param>
    18.      /// <param name="GotAnim"></param>
    19.      public void GetDoorState(bool Opened,Animator GotAnim)
    20.      {
    21.         DoorAnimator = GotAnim;
    22.  
    23.         CmdSendDoorStateToServer(Opened, DoorAnimator.transform.name);
    24.     }
    25.  
    26.     [ClientRpc]
    27.     private void RpcSendDoorStateToAll(bool DoorState,string DoorName)
    28.     {
    29.         //Updates the DoorOpened variable on all clients from the server.
    30.         DoorOpened = DoorState;
    31.  
    32.         GameObject.Find(DoorName).GetComponent<MeshRenderer>().material.color = Color.blue;
    33.         /*SINC THE STUFF IN THE CLIENTS*/
    34.         //Works if host does the color change but not if a client does it.
    35.     }
    36.     [Command]
    37.     private void CmdSendDoorStateToServer(bool DoorState,string DoorName)
    38.     {
    39.         //Update the server from a client.
    40.         RpcSendDoorStateToAll(DoorState, DoorAnimator.transform.name);
    41.     }
    42.  
    43. }
    44.  
    but it just does exactly the same thing.
     
    Last edited: Jul 14, 2015
  6. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    instead of a single object that networks all the doors, you could put a NetworkIdentity on each door. Then the ClientRpc would be routed directly to the door object, and doors could passed as arguments to commands from players.
     
  7. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Can you please explain further?
    I mean, i alredy added a networkIndentity but what do you mean by the clientRpc getting routed directly?
     
  8. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    CmdSendDoorStateToServer is ignoring DoorName and passing the name of "DoorAnimator.transform.name" through to the ClientRpc. is that intentional?
     
  9. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Actually.. No , it should be passing DoorName.
    That still doesnt fix my main problem anyway :(

    Actually, it kinda does...
    The clients can now also color the door but the color doesnt get synced back to the server though
     
  10. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    Try something like:

    Code (CSharp):
    1.  
    2. class Door : NetworkBehaviour
    3. {
    4.     [SyncVar(hook="OnOpen")]
    5.     public bool openState;
    6.  
    7.     public override void OnStartClient()
    8.     {
    9.         // set color here
    10.     }
    11.  
    12.     void OnOpen(bool newOpenState)
    13.     {
    14.         // set color here
    15.         openState = newOpenState;
    16.     }
    17. }
    18.  
    19.  
    20. class DoorPlayer : NetworkBehaviour
    21. {
    22.     [Command]
    23.     void CmdOpenDoor(NetworkInstanceId doorId)
    24.     {
    25.         GameObject doorObj = NetworkServer.FindLocalObject(doorId);
    26.         if (doorObj != null)
    27.         {
    28.             Door door = doorObj.GetComponent<Door>();
    29.             if (door != null)
    30.             {
    31.                 door.openState = true;
    32.             }
    33.         }
    34.     }
    35. }
     
  11. anueves1

    anueves1

    Joined:
    Jan 10, 2014
    Posts:
    36
    Thanks @seanr for all your help, i finally made it work.
    I am posting here the final code with animator syncing also, the purpose of all the color stuff was to get the hang of how
    networking works so now i have this 2 classes:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. public class Door : NetworkBehaviour
    6. {
    7.     [SyncVar(hook = "OnOpen")]
    8.     public bool openState;
    9.     public Animator DoorAnimator;
    10.  
    11.     public override void OnStartClient()
    12.     {
    13.         // set color here
    14.         DoorAnimator = GetComponent<Animator>();
    15.         DoorAnimator.SetBool("Opened", openState);
    16.     }
    17.  
    18.     void OnOpen(bool newOpenState)
    19.     {
    20.         // set color here
    21.         openState = newOpenState;
    22.         DoorAnimator.SetBool("Opened", openState);
    23.     }
    24.  
    25. }
    26.  
    And:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.Networking;
    4.  
    5. /// <summary>
    6. /// Class used for the networking of the door animator and door functions.
    7. /// </summary>
    8. public class NetworkUnlockSystem : NetworkBehaviour
    9. {
    10.     private Animator DoorAnimator;
    11.     [SyncVar]private bool DoorOpened;
    12.    
    13.  
    14.      /// <summary>
    15.      /// Used for getting the information that we need for each door.
    16.      /// </summary>
    17.      /// <param name="Opened"></param>
    18.      /// <param name="GotAnim"></param>
    19.      public void GetDoorState(bool Opened,Animator GotAnim)
    20.      {
    21.         DoorAnimator = GotAnim;
    22.         DoorOpened = Opened;
    23.  
    24.         CmdOpenDoor(DoorAnimator.GetComponent<NetworkIdentity>().netId,DoorOpened);
    25.     }
    26.  
    27.     [Command]
    28.     void CmdOpenDoor(NetworkInstanceId doorId,bool Opened)
    29.     {
    30.         GameObject doorObj = NetworkServer.FindLocalObject(doorId);
    31.         if (doorObj != null)
    32.         {
    33.             Door door = doorObj.GetComponent<Door>();
    34.             if (door != null)
    35.             {
    36.                 door.openState = Opened;
    37.             }
    38.         }
    39.     }
    40.  
    41. }
    Thanks for all the help provided!!