- Home /
Subtract variable value in certain seconds
----I WRITED MY CODE ON SPANISH WORDS, SORRY, MAYBE SOMEONE CAN HELP ME----
I am making a zombie shooter game, but i want to make 3 variables that have to be: Hambre = Hunger (im translating the words) Infeccion = infection Sed = Thirst (all 3 must be integrers) but i have the problem that the variables do not get subtracted by "1" every certain time every subtract acction i putted it on "start" function... soo, here is my code whit the error:
//GUI TEXTURAS
var guihambre:GUIText;
var guised:GUIText;
var guiinfeccion:GUIText;
//NUMEROS ENTEROS
var hambre:int;
var sed:int;
var infeccion:int;
var muriendo:int;
//BOOLEANS FALSO VERDADERO
var infectado:boolean;
var sediento:boolean;
var hambriento:boolean;
var accionar:boolean;
//NUMEROS FLOAT
var ratioinfeccion:float;
var ratiohambre:float;
var ratiosed:float;
var ratios:float;
var ratioh:float;
//VARIABLES EXTERNAS
var itemTracker;
//FUNCIONES
function Start () {
accionar=true;
itemTracker = GameObject.FindWithTag("Player").GetComponent(playermotor);
while (true) {
while (infectado==false) yield;
yield WaitForSeconds (ratioinfeccion);
itemTracker.hp-=infeccion;
}
while (true) {
while (hambriento==false) yield;
yield WaitForSeconds (ratiohambre);
itemTracker.hp-=1;
}
while (true) {
while (sediento==false) yield;
yield WaitForSeconds (ratiosed);
itemTracker.hp-=1;
}
while (true) {
while (accionar==false) yield;
yield WaitForSeconds (ratioh);
hambre-=muriendo;
}
while (true) {
while (accionar==false) yield;
Debug.Log("THE SUBTRACTED VALUES ARE WORKING");
yield WaitForSeconds (ratios);
sed-=muriendo;
}
}
function Update(){
guihambre.text=""+hambre+"%";
guiinfeccion.text=""+infeccion+"%";
guised.text=""+sed+"%";
if(hambre<0){
hambre=0;
}
if(hambre>100){
hambre=100;
}
if(hambre==0){
hambriento=true;
}else{
hambriento=false;
}
if(infeccion<0){
infeccion=0;
}
if(infeccion>100){
infeccion=100;
}
if(infeccion!=0){
infectado=true;
}else{
infectado=false;
}
if(sed<0){
sed=0;
}
if(sed>100){
sed=100;
}
if(sed==0){
sediento=true;
}else{
sediento=false;
}
}
Answer by fafase · Aug 16, 2012 at 07:41 AM
Ok if I got it right, you want your variables to be decreased every second.
I would go for timer:
var timerSed:float;
var timerInfeccion:float;
var timerHambre:float;
function Update(){
timerSed += Time.deltaTime;
timerInfeccion += Time.deltaTime;
timerHambre += Time.deltaTime;
if(timerSed > 1){
sed--;
timerSed = 0;
}
if(timerInfeccion > 1){
infeccion--;
timerInfeccion = 0;
}
if(timerHambre > 1){
hambre--;
timerHambre = 0;
}
}
That is obviously if you have those three variables independantely starting, in the case they all go together, you only need one timer and all get subtracted at once:
var timer:float;
function Update(){
timer += Time.deltaTime;
if(timer > 1){
sed--;
hambre--;
infeccion--;
timer = 0;
}
}
Thank you very much, it worked!. I think i should think on more easy ways to make my scripts because i was going on the wrong way. I did that on ActionScript 2.0 (FLASH 8) but i did never thought about doing it in Unity. By the way, is there a way to make it more faster?, may be ill need that later.