arjwolf
arjwolf

Reputation: 191

Changing Light Intensity Unity Multiplayer

I am currently facing an issue when trying to change the intensity of my light in multiplayer.

The light intensities changes fine for the person who starts the game, the host. However the people who connect to the host, their light intensity does not change.

I am trying to change the light intensity using the [SyncVar] however the player who connects to the host is not seeing the light intensity change at all. Here's my code:

 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();
     }
 }

My issue is that the light intensity is only changing for one player, the host. I want the light intensity to change for all player who connect to the game. Any suggestions are welcome.

Upvotes: 1

Views: 743

Answers (2)

arjwolf
arjwolf

Reputation: 191

I was able to fix this issue by completely removing the light intensity change logic from this class. This class now 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've gone ahead and put the condition to change the light intensity into my SpawnManager_waveLevel and now it works fine. I hope this will help whoever was facing the same issue as me.

oh and also a big change in that class was in the method OnLightAmountChange(), I no longer set lightAmout = amount; I now set the following as you can see light.intensity = amount; The actual intensity.

Upvotes: 0

Jared Merritt
Jared Merritt

Reputation: 97

Quite a simple fix - you aren't giving the script an alternative if 'isLocalPlayer' is false.

Here's a fixed light change for you:

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);
         }
     }
     else{
         // If not a local player, simply update to the new light intensity.
         light.intensity = Mathf.Lerp(light.intensity, lightAmout, fadeTime * Time.deltaTime);
     }
 }

Upvotes: 0

Related Questions