Multiplayer Light Intensity Not Syncing
Hi there I have a script I am currently having a difficulty with. Basically the script changes the intensity of the directional light in my scene.
The script works fine for the person who hosts the game but doesn't work for the people who connect to the hosts game.
I have a variable
level
This variable changes and I want to change the light intensity with it.
This is the script I have written:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class dayNightCycle : NetworkBehaviour { //changes day and night based on the wavelevel SpawnManager_waveLevel.cs script
Light light;
float fadeTime = 1f;
[SyncVar(hook = "OnLightAmountChange")]
float lightAmout = 0f;
SpawnManager_waveLevel level;
public override void OnStartLocalPlayer()
{
light = GetComponentInChildren<Light>();
level = GetComponent<SpawnManager_waveLevel>();
light.intensity = lightAmout;
}
// Update is called once per frame
void Update () {
changeLight();
}
void changeLight()
{
if (isLocalPlayer)
{
if (level.waveCounter == 1)
{
lightAmout = 0.03f;
light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime);
}
else
{
lightAmout = 1f;
light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime);
}
}
}
void OnLightAmountChange(float amount)
{
lightAmout = amount;
changeLight();
}
}
Answer by x00108663 · Mar 30, 2017 at 01:43 AM
I was able to create a solution to my question here and it looks like the following:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class dayNightCycle : NetworkBehaviour { //changes day and night based on the wavelevel SpawnManager_waveLevel.cs script
[SerializeField]
public Light light;
[SerializeField]
SpawnManager_waveLevel level;
[SyncVar(hook = "OnLightAmountChange")]
public float lightAmout = 0f;
public override void OnStartLocalPlayer()
{
OnLightAmountChange(lightAmout);
}
void OnLightAmountChange(float amount)
{
light.intensity = amount;
}
}
I am no longer adding the condition to change the light intensity in this class I am now doing it in the actual SpawnManager_waveLevel class.
Your answer
Follow this Question
Related Questions
UNET (Multiplayer) calling [command] function twice 0 Answers
How to make my game multiplayer? 1 Answer
Flip Character Direction Networking 0 Answers
[UNet] Network Animator transition stutters 0 Answers
Network.Connect is not working 1 Answer