unity – Why when setting a Light intensity value to 0 it’s not changing and stay on value 2?


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NaviLightsState : MonoBehaviour, IStateQuery
{
    public SaveLoad saveLoad;
    public DimLights dimLights;
    public UnlockCrate unlockCarte;
    public Light() naviLights;

    private State m_state = new State();
    private bool lightsDim = false;

    public Guid UniqueId => Guid.Parse("3D79CDBA-2E98-420A-9278-530869F80B6B");

    private class State
    {
        public float naviLightsIntensity;
        // To make the part of the naviLights to save the naviLights states save and load.
    }

    public string GetState()
    {
        return JsonUtility.ToJson(m_state);
    }

    public void SetState(string jsonString)
    {
        m_state = JsonUtility.FromJson<State>(jsonString);

        foreach(Light light in naviLights)
        {
            light.intensity = m_state.naviLightsIntensity;
        }
    }

    private void Update()
    {
        if(dimLights.lightsDimmed == true && lightsDim == false && unlockCarte.HasOpened() == true)
        {
            m_state.naviLightsIntensity = dimLights.lightsToDim(0).intensity;
            saveLoad.Save();

            lightsDim = true;
        }
    }
}

When I’m loading the saved game I can see in the saved game file on the hard disk that the saved intensity is 0 and also I’m using a breakpoint on this line in the foreach :

light.intensity = m_state.naviLightsIntensity;

And m_state.naviLightsIntensity value is 0.0 but when I’m back to the game I see that the lights intensity values are still 2 and not 0.

In the foreach loop I have two Lights components.

Navi lights before running the game

Before running the game by default both Light and Light1 intensity values are set to 2.

Then when loading :

Two lights components

And the value that I’m getting and setting to the lights :

The value that is getting and set to the lights is 0

But then when I’m making continue and return to the game I see the value of the lights is 2 :

The NAVI has changed parent as child now he is child of another parent then he was when the game was running but the lights intensity values still 2 and not 0. I don’t think so the fact that NAVI has changed parent has anything to do with the problem.

After loading the lights values are 2 and not 0