Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Steelshot · Mar 03, 2017 at 06:24 PM · c#variablesbooleanfloatif statement

Some Boolean "Action" Trouble

Okay, so in this script whenever the booleans IsOver5 and IsUnder5 are true, they do not do anything whenever the mouse clicks on the bakery. IsUnder5, when true, it is suppose to say in the console that the player does not have enough apples. Any help is appreciated!

Here is the code: void Update () {

         //States the booleans
         bool IsOver5 = Apples<= 5f;
         bool IsUnder5 = Apples >= 5f;
 
         //This changes the text
         txt.text = "Ray Distance: " + distance;
 
         //Basically declares where the ray is going
         Vector3 forward = transform.TransformDirection (Vector3.forward) * distance;
         Debug.DrawRay (transform.position, forward, Color.red);
 
         //Basically declaring Apples 
         if (Physics.Raycast (transform.position, (forward), out hit, 5.0f)) {
             rayDistance = hit.distance;
 
             if (hit.collider.tag == ("Apple")) {
 
                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
                     apples.text = "Apples: " + Apples;
                     Apples = Apples + 1;
                 }     
             }
             //Declares what the Bakery will do.
             //If tag is Bakery
             if (hit.collider.tag == ("Bakery")) {
                 
                 //When left mouse button is clicked
                 if (Input.GetKeyDown (KeyCode.Mouse0)) {
                     
                     //Booleans, if true?
                     if (IsOver5 == true) {
                         Apples -= 5f;
                     } 
                     if (IsUnder5 == true) {
                         print ("You Don't Have Enough Apples!");
                     } 
                 }
             } 
                 
                 
             }
         }
Comment
Add comment · Show 2
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 laszlar · Mar 03, 2017 at 06:30 PM 0
Share

Perhaps you didn't initialize Apples with a value?

avatar image laszlar · Mar 03, 2017 at 06:35 PM 0
Share

Also, I'm thinking Apples is a score, so you might need to save to PlayerPrefs.Save();

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by jjcrawley · Mar 03, 2017 at 06:46 PM

Your logic is incorrect, and the isUnder5 variable is unnecessary, if you have more than 5 apples then you can't have less than five apples.

 bool isOver5 = Apples >= 5f;

That will be sufficient, the extra isUnder5 will mess with the logic anyway. It will be true if you have 5 apples, the same as isUnder5. It will cause the code to remove five apples, before saying that you don't have enough. Debug it out and see for yourself.

Minor logic bug out the way. There are two things worth checking out. First the physics raycast, is it ever registering true. Place a debug inside and see. If that is working fine, double check your tags, make sure that your bakery object is tagged as Bakery. If that's okay, then check your Input check, place a debug in there.

Finally, if all of these are working, then check your apples. Are they being incremented correctly? You can check this by debugging out your isUnder5 flag. Do these checks in any order you like, may want to check the apples first before sorting out the raycast stuff.

Hopefully this'll lead you in the right direction.

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
avatar image
1

Answer by UnityCoach · Mar 04, 2017 at 01:50 AM

Ok, I sort of understand what you want to do. I'm not going to discuss your logic here, as I believe it's always an iterative process.

You can use an accessor, outside of Update, like this :

 bool isOver5 { get { return Apples >= 5f; } }

You may also want to simply use the condition in the statement itself, as it seems it's the only place you use it.

 if (Apples >= 5f)
 {
     Apples -= 5f;
 } 
 else
 {
     print ("You Don't Have Enough Apples!");
 }
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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to set a boolean to true, when pressing a button, and keep it without changing to false again,How to set a boolean true when pressing a button, without letting it change to false again? 3 Answers

if statement for infinity. How to check if a variable is Infinity? 3 Answers

(C#) If statements based on variables in another script 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