Forum rules - please read before posting.

Link behavior of all prefab instances to global variable

Hi,

I have a master light switch in one scene that will turn on/off multiple lights, spread in many scenes. I have a prefab where the lamp is the parent object and the point light is the child object. The point light has "Light Switch" and "Remember Light" components attached. When turning on the master switch, a Global Bool variable "Light" changes to True. I'd like to link this global variable to the Prefab and/or all scene instances of the prefab to turn on/off Emission of the lamp and turn on/off the light of the Point Light.

I've played around with custom events and also with a monobehavior script attached to the prefab of both lamp and point light that checks the AC global variable on Update, but I haven't been able to make it work. Is it possible to link the prefab of the lamp and the point light to the global variable in some way so that all instances in all scenes are turned off/on? Or at least in the current scene and I can put the action list in the "Run when loading scene" in all the other scenes? Will need to figure out how to turn on/off Emission through an Action though.

// Magnus

Comments

  • Yes, this is possible using parameters.

    What you can do is run an ActionList to update a light's state when the scene begins - either when opened naturally, or from loading a save-file. Because of this, you can remove the Remember Light component.

    Start by creating an ActionList asset file, and add a Variable: Check Action to check the state of the global light switch Bool variable. Follow both outputs up with an Object: Send message Action that pass "Turn On" message if True, and "Turn Off" if false (as normal when using the Light Switch component).

    However, instead of assigning the Light Switch in these Actions, define a GameObject parameter named "Light" in your ActionList Editor, and use it to override both of these Action's Object to affect Action. This will cause the value of the "Light" object to be used by the Actions when run.

    Next, attach an ActionList Starter component to your prefab, and assign the ActionList asset file. Check both Run on scene start? and Run on scene load?. Then check Set parameters?, and assign the Light child object as the "Light" parameter value underneath.

  • Thanks for this. I hadn't used the ActionList Starter component before. This works for lights outside the current scene when switching scenes, but how do I turn on the lights that are in the current scene when switching the master switch i.e. changing the global "MasterSwitch" variable? I assume through an event system where the prefab instances listen to the Global Variable in some way, but maybe I am on the wrong path..

  • They would need to be called manually. Is there a fixed number of lights in the same scene as the "master" switch, or are there dynamic elements?

    An ActionList Starter component can be run manually by invoking its RunActionList function. This can be done from within an ActionList by using the Object: Call event Action.

  • The number of lights are fixed, lots of different lamps and lights though, so I suppose I could group them all under a parent and SendMessage to all children. However, I have some other similar situations so I'd prefer to use/learn CallEvent.

    I tried to use CallEvent by invoking the RunActionList function(s) using the Light parent prefab as the object, but it doesn't work for me. All ActionLists are asset based.

    Please see below for screenshots of inspectors and action lists.

    https://imgur.com/dwMvKDo

  • As the Object: Call event Action relies on Unity's event system, it'll affect the object it directly references - in this case, the original prefab. If you're calling it from an asset file, you won't be able to have it reference the prefab's instance in the scene.

    This is different to the Object: Send message Action, which is able to use AC's Constant ID system to reference the scene instance. If you're calling it from an ActionList asset, this is the one you'll want to use.

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.