Scratching my head stuck for about 45 minutes, I’m missing something, can anyone give me a quick hand here?
using UnityEngine;
using System.Collections;
public class Bools : MonoBehaviour {
public bool amIDead = true;
public bool buriedInGround = true;
// Use this for initialization - going to use some condition statements here.
void Start () {
if (amIDead && buriedInGround) {
Debug.Log ("Yep, your dead and buried!");
}
else if (amIDead != buriedInGround) {
Debug.Log ("Nope! Your dead but not buried!");
}
else if (!amIDead & !buriedInGround) {
Debug.Log ("Your not dead OR buried!");
}
else if (buriedInGround |! amIDead) { //<--- I'm having a bitch of a time with this! Keeps returning "your dead but not buried"
Debug.Log ("You've been buried alive!!!");
}
}
// Update is called once per frame
void Update () {
}
}
Thanks tons folks!
Personally I like to rewrite the if condition clauses so they only ask one thing at a time, even if they ask the same thing twice. For instance:
if (amIDead) {
// at this point we know we're dead
if (buriedInGround)
{
// gap...
}
else
{
// gap...
}
}
else
{
// at this point we know we're alive
if (buriedInGround)
{
// gap...
}
else
{
// gap...
}
}
Now it becomes a lot easier to fill in the gaps I left above just by sort of following the if/else statements down the “tree” I made above.
Thanks Kurt and Rakk. The problem I’m having currently though is that last “else if” statement. Its giving me fits. Just trying to get my head around things, writing alot of silly code to try and get the swing of things. I’ve set the last condition to “not equal to (!=)” but for some reason it always reverts back to “Nope! Your Dead but not buried!”
Ok, when using bools and after doing some digging, Kurt’s method is the most logical way to set this up, as the way control is handed off to the next statement will not work the way I was trying to make it happen… You guys have been super helpful!
Pay attention to Line 16. Your condition is missing an &. Always do && or it might cause some problems later on…
elseif(!amIDead &!buriedInGround)