Forum rules - please read before posting.

Light Switch component for 2D lights?

Hey. Ive been trying to use the light switch as mentioned in many other threads but I cant get it to work with 2D lights.
Is there an updated script available or anyone have a better way of turning on and of 2dLights with Action lists?

Comments

  • Welcome to the community, @Wosie.

    You can instead rely on animation to control the properties of a 2D light.

    Attach an Animator component and assign an Animator Controller. Then create two animations (e.g. "on" and "off) for it that change the light the way you want.

    You can then use AC's Object: Animate Action, setting the Animation engine to Mecanim, to control the animation's playback - either by calling the animation by name directly, or changing an Animator parameter value to cause a transition.

    Then just attach a Remember Animator component to the light to have it's animation state stored in save games.

  • Thanks Chris! Ive been a long time lurker for sure. I'm very impressed with the support you provide here on a daily basis!

    I'll give it a go!

  • Hello, I'm using this exact method, but I'd need to access the animation through scripting.
    What would that code look like?

    The reason is my character is carrying a lamp, and I need the light to turn off as soon as she un-equips it. Unequiping in my game is done by clicking on a container so that the item transfers back to her inventory. This procedure is carried out through scripting.

  • AC isn't involved when animating an object through script. For this, you can just access the Animator component directly to control its parameter values, for example:

    GetComponent<Animator> ().SetBool ("IsOn", true);
    

    See Unity's docs here for more details.

    With v1.78, however, it may not be necessary to rely on scripting for this feature, as the new Scene Item component, and Inventory: Scene Item Action, allow for the transferring of items from the Inventory into the scene, and back. This can be followed with a Character: Hold object Action to attach the spawned object to the Player's hand.

    For details on this, see the Manual's "Scene Items" chapter.

  • Thanks for the advice regarding the new Scene Item actions.

    The problem is that when I click on a container, the only options are to transfer the items back to the inventory, or use a custom script. There's no option to run an action list. That's why I need to handle it with code.

  • I assume GetComponent can only be used in a script that's attached to the game object in question (=the light)?

    So if I already have a script that deals with whatever happens when I click on a container item, how do I refer to the light object?

    I'm guessing I can use something like a serialized field, and input the object in a slot in the inspector?

    I need a nudge in the right direction here...

  • GetComponent can be used on any GameObject variable you have access to, e.g:

    mySpawnedObject.GetComponent<Animator> ()...
    

    What is the script you're using?

  • It's the script you provided in this thread:
    https://adventurecreator.org/forum/discussion/11681/equipment-window/p2

    So in the function that checks if I click on the container in question (unequiping the object), I need to also disable the 2d light, since that doesn't disappear when the parent object is made invisible.

  • Sorry, I think I'm missing something.

    The StatUpdateExample script in that thread only deals with Variable values as you add/remove items from a Container. It doesn't reference any parent object or visibility state. Have you since modified it?

  • Yes, sorry for the confusion, I've added this under OnContainerRemove:

    if (container.gameObject.name == "HeadContainer")
    {

            GameObject[] gear = GameObject.FindGameObjectsWithTag("HeadGear");
    
    
            foreach (GameObject obj in gear)
            {
                obj.GetComponent<Renderer>().enabled = false;
    
            }
        }
    

    This is because every time I click on the head container, I want all objects that are currently visible (hats etc) to become invisible. This works perfectly as is.

    Now I just need to add something that turns off any 2d lights that are children of said objects (in this case, it's a head lamp).

  • Ok, I just realized I solved my own question here!
    Thanks for the help :)

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.