Hello!
Im new to this and Ive 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.
public class NetworkPlayer : NetworkBehaviour {
public GameObject FPSCamera;
public CharacterController characterControler;
public FirstPersonController FPScontroler;
//Related to the flashLight EachPlayer should carry. The "Luz" in this Script is the Flashlight ... yeah, named poorly.
[SerializeField] private Light Luz;
[SyncVar] private bool LightState;
[SyncVar] public int LightInt;
public static int LightExtern;
private void Start ()
{
if (!isLocalPlayer) {
FPSCamera.SetActive (false);
characterControler.enabled = false;
FPScontroler.enabled = false;
}
}
private void Update ()
{
GetLightValue ();
if (Input.GetButtonDown ("AniF"))
{
if (isLocalPlayer == true)
{
Luz.enabled = !Luz.enabled;
Debug.Log (LightState);
}
SendLightValue ();
}
if (Input.GetButtonDown ("AniE"))
{
if (isLocalPlayer == true)
{
AddTheTHING();
}
}
}
private void GetLightValue()
{
if (isLocalPlayer == false) {
Luz.enabled = LightState;
Debug.Log ("Not-Local Player from GetLightValue Executed");
} else {
Debug.Log ("Local Player from GetLightValue Executed");
}
}
[ClientCallback]
private void SendLightValue()
{
if (isLocalPlayer == true)
{
CmdSendLightValue();
Debug.Log ("Cliente Call Executed");
}
}
[Command]
private void CmdSendLightValue()
{
LightState = Luz.enabled;
Debug.Log("CmdSendLightValue Command Executed");
}
public void AddTheTHING()
{
if (isServer == true) {
LightInt++;
Debug.Log (LightInt);
Debug.Log ("Non-Server Method");
LightExtern = LightInt;
}
else
{
CmdAddTheTHING ();
}
}
void CmdAddTheTHING()
{
LightInt++;
Debug.Log (LightInt);
Debug.Log ("Server Command Method");
LightExtern = LightInt;
}
}
with this, each player only see their lights and the intensity also changes only for the local player pressing “E”.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class LightLocal : MonoBehaviour
{
public Light LightMono;
public int LightIntInput;
void FixedUpdate ()
{
LightIntInput = NetworkPlayer.LightExtern;
LightMono.intensity = LightIntInput;
}
}
This is how the global light is configured. tried to make it a network behavior earlier with no better results…
Thanks in advance!