- Home /
Need help with Enabling/Disabling GUITextures with script
My script is for a flash light, the idea is that when the variable energy goes below 50 the GUI of a green battery disappears. The issue I am having is that my script doesn't work with the drainspeed variable, the GUI doesn't disappear when it's drained below 50, however i wonder if this is an issue with the function only being read once...
var lightSource : Light; //Connect the light source in the Inspector
var bat1 : GUITexture;
static var energy : float = 60; //The energy amount of the flashlight
static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
private var drainSpeed : float = 10.0; //The speed that the energy is drained
function Update () {
if (Input.GetKeyDown(KeyCode.E)) ToggleFlashlight();
}
//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () {
turnedOn=!turnedOn;
if (turnedOn && energy>0) {
TurnOnAndDrainEnergy();
} else {
lightSource.enabled = false;
}
}
//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () {
lightSource.enabled = true;
while (turnedOn && energy>0) {
energy -= drainSpeed*Time.deltaTime;
yield;
}
lightSource.enabled = false;
}
//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) {
energy = Mathf.Clamp(energy+amount, 0, 100);
}
function Start(){
if(energy > 50){
bat1.enabled = true;
}
if(energy <50){
bat1.enabled = false;
}}
and this is my script
Answer by Joyrider · Aug 11, 2013 at 06:15 PM
You should call enabled and set it to false. Instead of calling disabled.
The disabled property doesn't exist on any type of object.
Thanks that removed the error but, I still don't know how to get the image to disappear when it's "drained" below 50, any ideas?
You should put the code you have in Start() (which is only executed once, at the very beginning) in your update function; that shoud do the trick.
do the same check you do at start, in function "TurnOnAndDrainEnergy "
Oh damn! Noob error! The amount of stupid I feel now passing through my body >.< !!! =D thanks a bunch=3
Your answer
Follow this Question
Related Questions
Show GUI texture then not 0 Answers
A Button, A Boolean and 2 Textures. 2 Answers
Wearied Error 1 Answer
gameObject.enabled is not working 1 Answer
Interactive pie-shaped GUI 3 Answers