Turning Lights On and Of

Updated on August 25, 2014 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
11 on August 23, 2014

Hey Guys,

I have next to no expierence with both coding and unity, well I´ve watched the “How to program in C#” toturials done by brackeys but I´m only slowly getting the hang of it. So what I´m trying to figure is a way to turn on streetlamps, or candles; whatever…
Kinda like turning on candles in amnesia: the dark descent. So I guess it´s got to do with raycasting, but what would be the best way to write some code to turn them on and of. Also I don´t know if I should give the models a light component, or give them emissive materials.

Thank´s in advance,
You could really make my day!

  • Liked by
Reply
2 on August 23, 2014

Im still kinda new to coding as well but I will share what I can. I made a little game a while back where the player was able to toggle on/off a flashlight. This is the code I used:

if(Input.GetButtonDown (“Flashlight”))
on = !on;

if(on)
light.enabled = true;
else if(!on)
light.enabled = false;

I suppose you could switch the if (Input…) to check if a raycast hits it instead.
Hope that can be of some use 🙂

on August 23, 2014

Thank you so much, I´l se how I can make use of that! (:

Master
on August 23, 2014

I believe if you use “.enabled” you get a yellow error in the console about it begin obselete.
to avoid this you can just use “light.SetActive(true);” instead

Show more replies
  • Liked by
Reply
Cancel
1 on August 23, 2014

Credit´s to Kalaskryss!!!
I´ve now got it working! The reason I´m still here is to ask if there would be a good way to set up the lighting. As far as my knowledge goes, turning lights on and of is dynamic lighting, and it seems to work with < 4 Lights. Do I need Unity Pro or is there another way to make many Lights “configurable”?

If you want to read the script, here it is:
This is the object That illuminates (It´s called ColorChangingCube):

#pragma strict
function TurnLightOf ()
{
 light.enabled = false;
}

And this is the script for the hands (basicly the script from the survival game series): 
#pragma strict
var TheDammage : int = 50;
var Distance : float;
var Hand : Transform;
var MaxDistance : float = 1.5;
var TheCube;
TheCube = GameObject.Find ("ColorChangingCube");
function Update ()
{
 if (Input.GetButtonDown("Fire1")) 
 {
 Hand.animation.Play("Punch");
 var hit : RaycastHit;
 if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
 {
 if (hit.collider.gameObject.Find ("ColorChangingCube"))
 {
 Distance = hit.distance;
 if (Distance < MaxDistance)
 {
 hit.transform.SendMessage("TurnLightOf", TheDammage, SendMessageOptions.DontRequireReceiver);
 }
 }
 }
 }
}

It´s propably horrible, but it works fine for me. Please note that my only goal was to be able to turn them of, if I wanted, I could´ve easily implemented to turn them on back again. nevertheless, this is what my code looks like. BTW thats the first script I´ve ever written.

on August 24, 2014

Realtime lighting over 4 lights is indeed a Unity Pro feature.

Its mostly why you tend to rely on baked lighting if your developing in the Unity Free environment.

Show more replies
  • Liked by
Reply
Cancel
1 on August 23, 2014

I don’t know if it makes any difference, but I don’t know why you have a GameObject.Find under your declarations & not in a function, I don’t think that would even get called…..  not that it matters because you have one in the raycast bit   ^_^

Well done for getting stuff to do things, it nice when that happens   ^_^

on August 24, 2014

Ya its very bloated, I know. Tons of unimportent stuff, that I don’t use. Thanks though (:

Show more replies
  • Liked by
Reply
Cancel
2 on August 24, 2014

woah you should take this line out:

 if (hit.collider.gameObject.Find ("ColorChangingCube"))

GameObject.Find is something best done under the Start or Awake functions. It’s expensive and not to be used very often. As it is, you’re calling it every frame

Wise
on August 24, 2014

Quite right……  best to use tags to see what the hit is.  Not sure how the send message works with that though.

on August 24, 2014
woah you should take this line out:

 if (hit.collider.gameObject.Find ("ColorChangingCube"))

GameObject.Find is something best done under the Start or Awake functions. It’s expensive and not to be used very often. As it is, you’re calling it every frameFrom Miziziziz

This, you don’t want to call this if statement every second, once you’ve found this object, create a bool, telling that you hit this object. And later, if you want to change the light, you can say something like 

if(hitGameObject == true){
        TurnLightOff();
}

Use whatever you re comfortable with.

Show more replies
  • Liked by
Reply
Cancel
0 on August 25, 2014

Ok Thanks again for all the help, the gameobject.find is now only called at the start function. I hope I´ve got it all right it still works tough. As I want to use well over 4 dynamic lights in the scene I´ve came along the idea of deactivating them all at the beginning, and hide them somehow, maybe through fog, so the game doesn´t have to draw them. As soon as the player hit´s a collider near them they will activate before the player can se them, so he get´s the feeling, that they were always shining.
I think this will be a way to trick around the fact that I only can use 4 dynamic lights
(evil genious masterplan (; )

Update 2:
Fixed it!:

#pragma strict
private var theCollider : String;
var Lighting; 
function Start ()
{
      light.enabled = false;
}
function LightisOff ()
{
       light.enabled = false;
}
function OnTriggerEnter ()
{
       if (Lighting == false)
       {
             light.enabled = false;
       }
       else
       {
             light.enabled = true;
       }
}
function TurnLightOf ()
{
       light.enabled = false;
       SendMessage ("LightisOff");
       Lighting = false;
}

Update 1:


It works! But as soon as I step out and enter the collider again, it activates the Light again. So heres the new code for the Light Cube:

#pragma strict
function Start ()
{
      light.enabled = false;
}
function LightisOff ()
{
      light.enabled = false;
}
function OnTriggerEnter ()
{
      light.enabled = true;
}
function TurnLightOf ()
{
      light.enabled = false;
      SendMessage ("LightisOff");
}

So how is there a way to tell the cube not to light up again once I´ve hit it? Is there maybe a way to prioritize my functions?
Thanks you all for helping me!

  • Liked by
Reply
Cancel