Change flashlight color when object is picked up,
I'm trying to make it so when you pick up a gameobject the spotlight color will change.
var lt: Light;
var MainCam : GameObject;
function Start () {
MainCam = GetComponent.<Light>();
}
function Update () {
if (Input.GetKeyUp(KeyCode.E))
lt.color -= Color.blue/ 2.0 * Time.deltaTime;
}
The code will not work, how can i fix it?,
You are setting your '$$anonymous$$ainCamera' (GameObject) as a light. Change this to 'lt' ins$$anonymous$$d.
Answer by bayerly · Mar 28, 2016 at 11:46 PM
Agreed with Namey5 (who puts lots of possible answers as Replies for some reason!):
MainCam is being declared as a GameObject, yet you're setting it as a Light component...Basically, I think I may see what you're getting at.
Rather than 'MainCam' you should probably say "var flashlight : GameObject;", then get its Light component (not sure if this is the most efficient way, but it's fairly readable). You assign the "flashlight" var in the Inspector or assign it in the Start() function somehow.
#pragma strict
var flashlight : GameObject;
function Update () {
if (Input.GetKeyDown("space")){
flashlight.GetComponent.<Light>().color = Color.blue;
}
}
It also looks like maybe you're trying to have it fade out or something afterward? Multiplying by time could produce strange results, and I'm not sure you can use a '-=' with regards to colors...or division. But I may be wrong.
If you're trying to have something that transitions between colors depending on what's going on, you might want to set up something with, say, Color.Lerp. A basic example:
#pragma strict
var flashlight : GameObject;
var lerpedcolor : Color;
var currentcolor : Color;
var changedcolor : Color;
var lerptime : float = 1.0;
function Update () {
flashlight.GetComponent.<Light>().color = currentcolor;
currentcolor = Color.Lerp(currentcolor, changedcolor, lerptime * Time.deltaTime);
if (Input.GetKeyDown("space")){
changedcolor = Color.blue;
}
if (Input.GetKeyDown("f")){
changedcolor = Color.white;
}
}
So Lerp and Slerp and other functions that transition between two values can be confusing...they took me a long time to figure out, and in this case Lerp may not be the best choice. But what's going on in this script is this - the flashlight's color is continuously listening to what the Lerp is doing. Then you change the values that the Lerp is using, and it moves toward "changedcolor".
Not sure if that's what you were looking for, but hopefully it'll help you out! Let me know if you have any questions.
Yeah, I was replying because a lot of the questions weren't really worth answering, i.e. it was too simple and they may have been looking for some broader approach. I just noticed that in this particular code, the light has already been declared at the top, it just isn't assigned and ins$$anonymous$$d the main camera is.
Your answer
Follow this Question
Related Questions
Make a relation between color and distance between objects' x coordinates 0 Answers
Copying or duplicating a gameObject from one scene to another 1 Answer
How can I get my camera to follow a clone after a certain trigger occurs? 2 Answers
how to enable a java script inside a gameobject on press of the space key? 1 Answer
can you control 2 gameobject color values without having to reference both ? 0 Answers