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 /
  • Help Room /
avatar image
0
Question by redstoner123 · Aug 30, 2015 at 11:59 AM · c#booleanboolif

how to boolean global and trigger a command when two booleans are true C#

I have a problem with my code

 using UnityEngine;
 using System.Collections;
 
 public class finish1 : MonoBehaviour
  {
         public bool Lock = false; 
     public bool Lock1 = false;
 
         void OnCollisionEnter (Collision col)
     {
         if (col.gameObject.name == "DeathCube") {
             Destroy (col.gameObject.GetComponent<Ctrl> ());
             Lock1 = true;}
             
             
         if (Lock1 == true) { if (Lock == true){Application.LoadLevel("game 2");};
         }
     
     }
 
 }

im trying to see if both lock1 and lock two are true using an if statement but when i start my game when only DeathCube collided game 2 starts

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 Dave-Carlile · Aug 30, 2015 at 01:08 PM

You're not clear about what the problem is, but the code you have should execute the LoadLevel when both Lock and Lock1 are true. But where is Lock set to true?

Also, two things about booleans. They are always true or false, which means you can use them directly in if or other statements that require a conditional (`while`, do, etc.) without comparing them to true or false.

So your code can be simplified like this:

 if (Lock1)
 {
   if (Lock)
   {
     Application.LoadLevel("game 2");
   }
 }


The second thing about booleans is that you can do boolean operations on them, such as and, or, not. So your two if statements can be combined:

 if (Lock1 && Lock)
 {
   Application.LoadLevel("game 2");
 }


&& means both Lock1 and Lock must be true in order for the if conditional to be true.

Comment
Add comment · Show 6 · 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 redstoner123 · Aug 30, 2015 at 02:24 PM 0
Share

what do you mean lock set to true??

avatar image Dave-Carlile · Aug 30, 2015 at 03:34 PM 0
Share

You have two variables, Lock and Lock1. You set Lock1 to true in OnCollisionEnter, but none of the code you show ever sets Lock to true. Both must be true in order for your LoadLevel to be called.

avatar image redstoner123 · Aug 30, 2015 at 05:02 PM 0
Share

i have a other piece of code that makes lock to true it is this using UnityEngine; using System.Collections;

public class finish1 : $$anonymous$$onoBehaviour { public bool Lock = false; public bool Lock1 ;

      void OnCollisionEnter (Collision col)
  {
      if (col.gameObject.name == "DeathCube") {
          Destroy (col.gameObject.GetComponent<Ctrl> ());
          Lock = true;}
          
          
      if (Lock1 == true) { if (Lock == true){Application.LoadLevel("game 2");};
      }
  
  }
 

}

avatar image Dave-Carlile · Aug 30, 2015 at 05:17 PM 0
Share

It's possible one of those isn't happening. You can add Debug.Log messages right before setting Lock = true and Lock1 = true to make sure that code is getting executed.

avatar image redstoner123 · Aug 30, 2015 at 06:05 PM 0
Share

i did do that and i did this

{

     public bool Lock ; 
     public bool Lock1 = false;
     
     void OnCollisionEnter (Collision col)
     {

         if (col.gameObject.name == "DeathCube1") {
             Destroy (col.gameObject.GetComponent<Ctrl> ());
             Lock1 = true;}
         
         
         
     if (Lock1)
     {
         Debug.Log( true );
         if (Lock)
         {
             Debug.Log( "check" );
             Application.LoadLevel("game 2");
         }
     }    

     
     


}

and this

     public bool Lock = false; 
 public bool Lock1;
     
     void OnCollisionEnter (Collision col)
     {
         if (col.gameObject.name == "DeathCube") {
             Destroy (col.gameObject.GetComponent<Ctrl> ());
         Lock = true;}
         
         

     if (Lock)
     {
         Debug.Log( true );
         if (Lock1)
         {
             Debug.Log( "check" );
             Application.LoadLevel("game 2");
         }
     }

 }

}

they both debugged true but neither debugged check ikd what this means

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Static bool 1 Answer

Referenced Bool is not updating 1 Answer

check bool across a script 1 Answer

how do i say this 1 Answer

My Bool function is returning False but it should be true in my opinion. Csharp C# 0 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