Forum rules - please read before posting.

How to "Remember" Global Lights?

edited August 2022 in Technical Q&A

Hi there, I am having a problem with Global Lights and Saving the states of them on my game .

There´s a particular Level that consists of three different "areas" -the main office, a bathroom, and a Bedroom.

To transition them I use Camera Switching and Fades, Player Teleporting and Navmesh changes and it works flawlessly.

The problem comes from the use Global Lights (Point Lights gives us no trouble) on each of them areas, we use slightly different setting of Global Lights to give them a distinct look and turn them On/Off (by disabling the GameObject through "Call Event " Action) when entering/leaving said area, the problem with Global Lights it´s that each one has to affect different Sorting Layers otherwise it gives Console Errors "More than one Global Light affecting this Sorting Layer"

This all works great, except than there´s no "Remember Light" component or anything (I believe) that allows me to save the state of this Global Lights on Save Slots.


Here´s an (hopefully useful) screenshot of what I mean.

I realize I have a way of fixing this, one is to re-organize my Layers so they don´t overlap and I don´t have to turn them on/off through AList, but this will be time consuming and tedious.

Is there a way to save gameObject Active/Inactive states? or maybe a Remember Component that affects Lights?

Thanks in advance!

Comments

  • Remember components won't be recognised by the save system if they're attached to disabled GameObjects. If you go down that route, I'd recommend controlling their enabled state via a parent GameObject that can handle the enabling/disabling.

    One way to do this would be to attach an Animator to a shared parent object, and supply it with animations that each turn one light on and another off. You could then control it via the Object: Animate Action, and attach a Remember Animator object to save the state in save-games.

    there´s no "Remember Light" component or anything

    The creation of a Remember Light component is covered in this tutorial on writing custom Remember scripts. If the lights you're referring to are URP then you may have to tweak it slightly, but it works by enabling/disabling the Light component itself, rather than the entire GameObject.

  • edited August 2022

    Would this Remember Light component disable Global Lights too?
    The Animate approach won´t help me, since the Lights would still conflict with the Sorting Layers cause they are all "On" at the same time.

  • Would this Remember Light component disable Global Lights too?

    Yes - you just need to tweak the script to refer to URP instead:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.Experimental.Rendering.Universal;
    
    namespace AC
    {
    
        [RequireComponent (typeof (Light2D))]
        public class RememberLight : Remember
        {
    
            public override string SaveData ()
            {
                LightData lightData = new LightData ();
                lightData.isOn = GetComponent <Light2D>().enabled;
                lightData.objectID = constantID;
    
                return Serializer.SaveScriptData <LightData> (lightData);
            }
    
    
            public override void LoadData (string stringData)
            {
                LightData data = Serializer.LoadScriptData <LightData> (stringData);
                if (data == null) return;
                GetComponent <Light2D>().enabled = data.isOn;
            }
    
        }
    
        [System.Serializable]
        public class LightData : RememberData
        {
            public bool isOn;
            public LightData () {}
        }
    
    }
    

    The Animate approach won´t help me, since the Lights would still conflict with the Sorting Layers cause they are all "On" at the same time.

    The animation approach allows you to disable GameObjects entirely.

  • That seemed to work just fine, the "LightSwitch" script that the tutorial mentions was not necessary though.

    Thanks Chris!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.