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
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.
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.
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");};
}
}
}
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.
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
Your answer
Follow this Question
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