Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sanfelipe007 · Aug 16, 2012 at 07:34 AM · variableyieldstartwhilesubtract

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;
 }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;
   }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sanfelipe007 · Aug 16, 2012 at 08:05 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

"yield return StartCoroutine" in Start function 1 Answer

How can I have a while(www is loading) loop? 1 Answer

Yield position within while loop in coroutine 1 Answer

Declaring a variable in Start funciton 3 Answers

Nothing happening after WaitForSeconds C# 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges