Hi, I have attempted to write a code that will drain the battery of the torch (it is just a normal point light) and there is no errors, it is just not doing anything, so what is wrong with this script?
var LightIsOff : System.Boolean;
var Recharge : int = 0;
var LightIntensity : int;
function update(){
LightIntensity = light.intensity;
CheckLight();
if(LightIsOff == false){
light.intensity -= 0.001;
}
if(LightIsOff == true){
if(Recharge == 1.5){
}else{
Recharge += 0.001;
}
}
if(Input.GetMouseButtonDown(2) && LightIsOff == false){
light.intensity = 0;
}
if(Input.GetMouseButtonDown(2) && LightIsOff == true){
light.intensity = Recharge;
}
}
function CheckLight(){
if(light.intensity == 0){
LightIsOff = true;
}else{
LightIsOff = false;
}
}
Thank you
When writing a script in Unity3D You don’t have to use System.Boolean
you can just set it as true or false since JavaScript already defines the variable in runtime for you.
Also. When you are accessing an object you can’t just say as you are defining:
light.intensity
You have to have some form of access to the actual object. If you are having the script attached to the light itself. Use the Transform class in which is the object the script is attached to itself and it would look a bit like this:
transform.light.intensity = 0.1;
Since the actual object isn’t a light, it has the script Light attached to it.
Also: You don’t need the function:
function CheckLight();
You can rea a global variable like that without having to update it. It will self update.
Also when adding a recharge you have to have some sort of increment in time so I would recommend looking into the Timing class.