Search Unity

Basic Syncronization issue!

Discussion in 'Multiplayer' started by Infinity_way, Jul 23, 2017.

  1. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    Hello!

    I`m new to this and I`ve been having issues to make simple things as synchronizing a flashlight or even make a global light change its intensity to all players. I've searched a little to see if i could find other threads with this problem but most of them were unanswered or too old.

    Code (CSharp):
    1. public class NetworkPlayer : NetworkBehaviour {
    2.  
    3.     public GameObject FPSCamera;
    4.     public CharacterController characterControler;
    5.     public FirstPersonController FPScontroler;
    6.  
    7.     //Related to the flashLight EachPlayer should carry. The "Luz" in this Script is the Flashlight ... yeah, named poorly.
    8.     [SerializeField] private Light Luz;
    9.     [SyncVar] private bool LightState;
    10.  
    11.     [SyncVar] public int LightInt;
    12.     public static int LightExtern;
    13.  
    14.  
    15.  
    16.     private void Start ()
    17.     {
    18.  
    19.         if (!isLocalPlayer) {
    20.             FPSCamera.SetActive (false);
    21.             characterControler.enabled = false;
    22.             FPScontroler.enabled = false;
    23.         }        
    24.     }
    25.  
    26.     private void Update ()
    27.     {      
    28.         GetLightValue ();
    29.  
    30.             if (Input.GetButtonDown ("AniF"))
    31.             {
    32.                
    33.                 if (isLocalPlayer == true)
    34.                 {
    35.                     Luz.enabled = !Luz.enabled;              
    36.                     Debug.Log (LightState);
    37.                 }              
    38.                 SendLightValue ();
    39.             }
    40.             if (Input.GetButtonDown ("AniE"))
    41.             {
    42.                 if (isLocalPlayer == true)
    43.                 {
    44.                
    45.                 AddTheTHING();
    46.                 }
    47.         }
    48.  
    49.  
    50.     }
    51.     private void GetLightValue()
    52.     {
    53.         if (isLocalPlayer == false) {
    54.             Luz.enabled = LightState;
    55.             Debug.Log ("Not-Local Player from GetLightValue Executed");
    56.         } else {
    57.             Debug.Log ("Local Player from GetLightValue Executed");
    58.         }
    59.     }
    60.  
    61.     [ClientCallback]
    62.     private void SendLightValue()
    63.     {
    64.         if (isLocalPlayer == true)
    65.         {
    66.             CmdSendLightValue();
    67.             Debug.Log ("Cliente Call Executed");
    68.         }
    69.     }
    70.  
    71.     [Command]
    72.     private void CmdSendLightValue()
    73.     {
    74.         LightState = Luz.enabled;
    75.         Debug.Log("CmdSendLightValue Command Executed");
    76.     }
    77.  
    78.     public void AddTheTHING()
    79.     {
    80.         if (isServer == true) {
    81.  
    82.             LightInt++;
    83.             Debug.Log (LightInt);
    84.             Debug.Log ("Non-Server Method");
    85.             LightExtern = LightInt;
    86.  
    87.         }
    88.         else
    89.         {
    90.             CmdAddTheTHING ();
    91.         }
    92.  
    93.     }
    94.     void CmdAddTheTHING()
    95.     {
    96.         LightInt++;
    97.         Debug.Log (LightInt);
    98.         Debug.Log ("Server Command Method");
    99.         LightExtern = LightInt;
    100.     }
    101. }
    with this, each player only see their lights and the intensity also changes only for the local player pressing "E".


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class LightLocal : MonoBehaviour
    7. {
    8.  
    9.     public Light LightMono;
    10.     public int LightIntInput;
    11.  
    12.     void FixedUpdate ()
    13.     {
    14.         LightIntInput = NetworkPlayer.LightExtern;
    15.         LightMono.intensity = LightIntInput;
    16.  
    17.     }
    18. }
    19.  
    This is how the global light is configured. tried to make it a network behavior earlier with no better results...

    Thanks in advance!
     
  2. Infinity_way

    Infinity_way

    Joined:
    Mar 21, 2017
    Posts:
    17
    ... i doubled this thread cuz i couldn't find my previews one, how do i delete this?