Forum rules - please read before posting.

[SOLVED] Turning Light On & Off

edited April 2014 in Technical Q&A
I've been looking at how to use the SendMessage or Visibility actions to turn lights on and off. Using the Object:SendMessage and trying to turn on a light doesn't work. 

Am I right in assuming I'll need a custom script for this? If so, what do I do?

Would it be possible to add a Light:TurnOn/Off action in a future update?

Comments

  • I am also trying to do that. Create a kind of alarm light flashing when the AC variable RoomLock is true. So far I am here :

    public class ChangeColor : MonoBehaviour {
        public bool RoomLock;
        public Color colorA;
        public Color colorB;
        public float speed;
    //    public Material colorMaterial;
        public float intense;
        // Use this for initialization
        void Start () {
       
        }
       
        // Update is called once per frame
        void Update () {

            RoomLock = GameObject.FindWithTag (Tags.persistentEngine).GetComponent ().GetVarValue (2);
            if(RoomLock == true)
            {
                // TRUE
               
                // Color color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time * speed, 1));
                //colorMaterial.SetColor("_Color", color);
                light.color = Color.Lerp(colorA, colorB, Mathf.PingPong(Time.time * speed, 1));
                light.intensity = intense;

               
            }
            else
            {
                // FALSE
               
                light.intensity=0;
            }

        }
    }


    but i get the error  Assets/Scripts/ChangeColor.cs(19,75): error CS0411: The type arguments for method `UnityEngine.GameObject.GetComponent<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly

  • edited April 2014
    Some time ago, I found this useful script modification, made by Chris. You have to modify/create new script from the actual ParticleSwitch script with a little modification like in this threat:


    For me worked with lights! Hope it helps you.
    Bye
  • edited April 2014
    thanks @2finger I tried it and i get an error
    SendMessage Switch(true) has no receiver!
    UnityEngine.GameObject:SendMessage(String)

    I am using object Send message, Message to Send: custom and Method name Switch(true)
  • @Antonis: try with a simple TurnOn and TurnOff, not custom. 
    Attach the modified script at your light (I called it: ParticleSwitchLight.cs) and when you make SendMessage, select the light and TurnOn. 
    If you can't make it works, I'll send you my script, but this evening because I'm in office now. ^_^
    Let me know!
  • edited April 2014
    This is the script I am using :

    Public class ChangeColor : MonoBehaviour {

       
        private void Switch (bool turnOn)

        {
            if (GetComponent <Light>())
            {
                GetComponent <Light>().enabled = turnOn;
           
            }


        }

        // Use this for initialization
        void Start () {
       
        }
       
        // Update is called once per frame
        void Update () {


               
        }
    }

    I've tried both ways via talking to the script by sending message to method Switch(true) and false

    and to ignore the script and just use TurnOn and TurnOff as a message to the light object.

    in both cases i get either Switch(true) has no receiver or TurnOn has no receiver!

    I would appreciate any help @2finger cause it is very crucial to be able to talk to functions and variables from AC to the rest of Unity.
  • Sorry @Antonis, but i'm not a programmer and I don't know any code. I reuse the ones others made... If this script gives you an error, I can't suggest you other solutions...

    But you have to tun on and off lights objects, like in the subject tread of TayannaStudios? This evening I'll take a look in my project, but I used it for some lights to turn on and off without problems...
  • edited April 2014
    I got it working following what 2finger suggested from the other forum. I simply duplicated the ParticleSwitch.cs script and called it a different name (ParticleSwitchLight.cs) and amended the section highlighted by Chris and applied it to the lights I needed to interact with.

    Calling Object:SendMessage and turning the lights On/Off now works fine.

    Thank you so much!
  • Glad to help you!  ;)
  • :( I don't really know why i get error message there is no receiver! can you share the code Tayanna Studios?

  • edited April 2014
    Have you attached the script to the light object?
    Click on the light you have to turn on and off, click Add Component in the Inspector window and add this script. 
    Then, you can command the light with Object: Send message
    Object to affect: your light with the script attached (!)
    Message to send: Turn On.

    using UnityEngine;
    using System.Collections;

    public class ParticleSwitchLight : MonoBehaviour
    {
    public bool enableOnStart = false;
    private void Awake ()
    {
    Switch (enableOnStart);
    }
    public void TurnOn ()
    {
    Switch (true);
    }
    public void TurnOff ()
    {
    Switch (false);
    }


    public void Interact ()
    {
    if (this.particleSystem)
    {
    this.particleSystem.Emit (this.particleSystem.maxParticles);
    }
    }
    private void Switch (bool turnOn)
    {
    if (GetComponent <Light>())
    {
    GetComponent <Light>().enabled = turnOn;
    }
    }
    }

    Let us know!
    Bye
  • Thanks a lot @2finger I will check it out first thing tomorrow
  • edited April 2014
    Ok @2finger i pass the value to boolean variable RoomLock in AC to true and then send an Object Send Message , using the method Switch(RoomLock) from AC and i get an error : SendMessage Switch(RoomLock) has no receiver! :( but IT WORKS ANYWAY :) strange
  • Yeah! Finally!  :)>-
  • Thanks man for taking the time, i still have to find out what ...has no receiver means
  • No problem Antonis! Good luck for your search! ;-)
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.