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 /
  • Help Room /
avatar image
0
Question by kenwiggins93 · Jun 05, 2020 at 11:58 PM · scripting problemscripting beginnerif-statementsboolbooleans

Issue with if-statements requiring two conditions.

Hey. So I'm trying to build a rest mechanic where the character falls asleep if they use too much energy, and then regains it while they are sleeping. My plan then was to make a simple Bool of 'is character sleeping' and then use an if statement to kick in the code. However, this doesn't seem to work and I can't figure out why.

The problem seems to be the && X portion, as when I remove that condition the code works and the int increases as it should. Any ideas of how to fix this? I've posted the whole script for clarity but only the //ENERGY is relevant, everything else is working fine.

 public class Otto_Data : MonoBehaviour
 {
     public static int health = 1000;
     public static int energy = 1000;
     public int smart = 1;
     public static int exercise = 1;
     public int clam = 1000;
 
     //FOOD
     public static int foodValue = 500;
     public Text foodText;
 
     //Health
     private bool isOttoDead;
     public Text ottoHealth;
     public GameObject dead;
 
     //ENERGY
     private bool isOttoSleep;
     public Text ottoEnergy;
     public GameObject sleep;
     protected float energyTimer;
 
 
     //FOOD
     private bool isOttoFull;
     private bool isOttoHungry;
     private bool isOttoSatited;
     public GameObject hungry;
     public GameObject full;
 
     protected float foodTimer;
     public int delayAmount = 1;
 
     // Start is called before the first frame update
     void Start()
     {
         dead.SetActive(false); //hide otto dead message. 
         hungry.SetActive(false); //hide 'hungry'.
         full.SetActive(false); //hide 'full'
         sleep.SetActive(false); // hide sleep
         isOttoSleep = false;
 
 
 
     }
 
     // Update is called once per frame
     void Update()
     {
         //TIMER
 
         foodTimer += Time.deltaTime;
         energyTimer += Time.deltaTime;
 
         //FOODTIMER
         if (foodTimer <= delayAmount)
         {
             foodTimer = 0f;
             foodValue--;
             foodText.text = "Food: " + foodValue;
         }
 
      
 
         
 
 
 
         // HEALTH
 
         ottoHealth.text = "Health: " + health;
 
         if (health == 0)
         {
             isOttoDead = true;
             Debug.Log("Otto is Dead :'(");
             dead.SetActive(true);
             //gameover
 
         }
 
 
 
         // ENERGY
        
             ottoEnergy.text = "energy: " + energy;
 
             if (energy <= 0)
             {
                 isOttoSleep = true;
                 Debug.Log("Otto is Sleeping :'(");
                 sleep.SetActive(true);
 
             }
             else
             {
                 isOttoSleep = false;
                 sleep.SetActive(false);
             }
 
             if (isOttoSleep == true) //debug
         {
             Debug.Log("otto is sleeeepping");
         }
 
             //energytimer
             if (energyTimer <= delayAmount && isOttoSleep == true)
             {
                 energyTimer = 0f;
                 energy++;
                 ottoEnergy.text = "energy: " + energy;
                 Debug.Log("energy increase");
             }
 
 
 
 
      
 
 
         // FOODCONDITIONS
 
 
 
         if (foodValue > 500)
         {
             isOttoFull = true;
             Debug.Log("Otto is Full");
         }
 
 
         if (isOttoFull == true) //full
         {
             full.SetActive(true);
         }
         else
         {
             full.SetActive(false);
         }
 
 
         if (foodValue < 100) //hungry
         {
             isOttoHungry = true;
             Debug.Log("Otto is Hungry");
         }
 
       
             if (isOttoHungry == true)
             {
                 hungry.SetActive(true);
                 health--;
             }
             else
             {
                 hungry.SetActive(false);
             }
 
         if (foodValue >= 101 && foodValue <= 499) //Satited
         {
             isOttoHungry = false;
             isOttoFull = false;
             isOttoSatited = true;
             Debug.Log("Otto is Satited");
             hungry.SetActive(false);
             full.SetActive(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
0

Answer by streeetwalker · Jun 06, 2020 at 03:08 AM

@kenwiggins93, the code logic structure makes it difficult to follow. It's a mess. If it were me I would not be setting sleep and then doing sleep in separate if statements - consolidate them into a set of nested if statements. (the food logic suffers from similar issues).

Here is some pseudo code:

 if( ! otto _sleeping ) { // is Otto not sleeping this frame?
     if( energy <= 0 ) { // is Otto ready to sleep this frame?
           otto _sleeping = true;
           sleep_delay = 0;
           sleep.SetActive(true); // on the next frame, Otto will sleep.
     }
 }else{ // Otto is already sleeping during this frame
     // Important: because Update is not guaranteed to run at a consistent 
     // frame rate without using delta time Otto may not sleep for a 
     // set amount of time, you must use delta time in both 
     // the sleep delay and the energy calculations!
     sleep_delay += Time.deltaTime
   
     // sleep_energy_gain is the amount of energy Otto gains 
     //  per second while sleeping - could be initialized outside of 
     // the Update function
     energy += sleep_energy_gain * Time.deltaTime;
  
     // sleep_max_time is how long should Otto sleep in seconds
     // must be initialized outside of the Update function
     if( sleep_delay >= sleep_max_time ) {  // is Otto done sleeping?
            otto _sleeping = false;
            sleep.SetActive(false); // on the next frame Otto wakes up
     }
 }



If Otto can't be killed while he is sleeping (or even if he can), probably an better way to do sleep that requires some kind of timer is to use a coroutine that uses "return yield WaitForSeconds" to effect the delay. That way you can do away with the whole mess

Comment
Add comment · 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

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

310 People are following this question.

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

Related Questions

Tutorial 3.1-7 "&& isOnGround == true" 0 Answers

I am trying to reference a bool from another script but the only value that it returns is false 0 Answers

Toggle direction 0 Answers

Flashlight flickering script? 0 Answers

Why does (GO).transform.position.Set(x,y,z); not work, while (GO).transform.position = new Vector3(x,y,z) does? 1 Answer


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