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 Jan_Julius · Sep 01, 2014 at 02:25 PM · booleanbooleans

How can I make this work when the booleans are equal?

When I run this code

         public void checkForMatches() {
         if(a && b 
            || a && c 
            || b && c 
            || a && b && c){
             matchFound();
 
             }
         }
     void matchFound(){
             Score += 10;
             }

it happens the frame after the booleans aren't true anymore but when used in update it's very inaccurate and sometimes adds 20 or 30 to the score, fixedUpdate seems to do the same thing.

Comment
Add comment · Show 4
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 Paulo-Henrique025 · Sep 01, 2014 at 02:26 PM 0
Share

Isn't this just a case of setting the bools false as soon as you add score? Or call CheckFor$$anonymous$$atches only when the player interacts.

avatar image Jan_Julius · Sep 01, 2014 at 02:40 PM 0
Share

It is done like so:

an object has 3 objects connected to it when something happens with these they send "Cylinder$$anonymous$$essage(id)" to the parent object which will then according to the id integer set the a, b or c boolean to true.

done like so:

     void FixedUpdate () {
         Color objectColor = renderer.material.GetColor("_Color");
         RaycastHit hit;
         Vector3 angle0 = transform.TransformDirection(Vector3.up);
         if (Physics.Raycast(transform.position, angle0, out hit, 2)){
             Color otherObjectColor = hit.collider.renderer.material.GetColor("_Color");
             //print("hit something");
             if(objectColor == otherObjectColor){
                 if(objectColor == Color.green && otherObjectColor == Color.green){
                     Execute$$anonymous$$atches ("Green");
                 } else if(objectColor == Color.blue && otherObjectColor == Color.blue){
                     Execute$$anonymous$$atches ("Blue");
                 } else if(objectColor == Color.red && otherObjectColor == Color.red){
                     Execute$$anonymous$$atches ("Red");
                 }
             }
         }
     }
     void Execute$$anonymous$$atches(string cylColor) {
         $$anonymous$$olecule molecule;
         molecule = transform.parent.gameObject.GetComponent<$$anonymous$$olecule>();
         if(cylColor == "Green"){ molecule.Cylinder$$anonymous$$essage(1); }
         if(cylColor == "Blue"){ molecule.Cylinder$$anonymous$$essage(2);}
         if(cylColor == "Red"){ molecule.Cylinder$$anonymous$$essage(3);}
     }

then in "molecule.cs"

     public void Cylinder$$anonymous$$essage(int id){
 
         switch(id){
             case 1:
             a = true;
                 break;
             case 2:
             b = true;
                 break;
             case 3:
             c = true;
                 break;
             }    
         }
avatar image Baste · Sep 01, 2014 at 03:37 PM 0
Share

Is there any specific reason why you can't set a, b and c to false after adding points?

 void matchFound(){
   Score += 10;
   a = false;
   b = false;
   c = false;
 }

Also, the last part of the 'if' is not necessary:

a && b && c

Since you have already checked a&&b, b&&c and a&&c, either a true result has already been found, or a&&b&&c is guaranteed to be false

avatar image Jan_Julius · Sep 02, 2014 at 07:39 AM 0
Share

The problem is that I need it to happen on the frame it happens, not afterwards or when it's set back to false but I need it to happen only once and the update in unity continiously puts 20 or 30 ins$$anonymous$$d of 10.

EDIT: I seem to have fixed it, I don't know how but it seems to be fixed >_<

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Suddenly_Bacon · Sep 01, 2014 at 04:42 PM

Maybe

 if(a && b || a && c || b && c || a && b && c)

should be

 if((a && b) || (a && c) || (b && c) || (a && b && c))

This will make it so that the AND statements will be calculated first, then the OR statements will correctly compare the AND statements.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Project Not working after 1 day and no changes. 1 Answer

Unity says there is no such thing as a boolean. 1 Answer

How to instantiate once when boolean changes 1 Answer

How to activate a panel by a toggle using a script? 1 Answer

void cannot be used in a boolean context 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