- Home /
Light problems
Hello im having a problem with some errors i get that i do not understand why im getteing. im posting a picture of my unity and my code below. this code should be able to turn on and off a torch. but i cant get it to work with the ligt.
The code is following:
//Light script on / off
var lightNr : int = 0;
var OffLight : float = 0.0;
var OnLight : float = 2.0;
private var light : GameObject;
function Start()
{
if(lightNr == 0)
{
isOn = true;
light.intensity = OnLight;
}
else
{
isOn = false;
light.intensity = OffLight;
}
}
function TurnlightOn(nr : int)
{
if(nr == lightNr)
{
isOn = true;
light.intensity = OnLight;
}
}
function TurnlightOff(nr : int)
{
if(nr == lightNr)
{
isOn = false;
light.intensity = OffLight;
}
}
¨
I hope someone can put some light in my dark why this is not working.
Thanks alot for using your time on reading my question.
Answer by Berenger · Feb 12, 2012 at 02:12 AM
That's a little javascript trap you got here. As you don't need to write everything, you don't see that everything you write is part of a class inheriting from MonoBehaviour, which inherit from Component. Component have a property called light, without majuscule, which return GetComponent(Light). By calling your variable light, Unity is lost between your variable and Component's property. Give it another name and you'll be fine.
Hello thanks for your answer im just not sure that i understand it. you are saying give "it" another name. what do you mean by it ?
im aprisiating you taking your time to help me :)
change "private var light : GameObject;" into "private var whateverNameYouLike : GameObject;"
Your answer
Follow this Question
Related Questions
Elevator script problem (Java) 1 Answer
Disable GameObject Only Father Not Children 2 Answers
GUI.Box within a Collider 1 Answer
Why is my script still running in other scenes? 1 Answer
Drag Script Not Working 1 Answer