How Do I Swap This Boolean?

I’ve got a problem… I can’t figure out how to make this boolean false whenever I am not triggering!

using UnityEngine;
using System.Collections;

public class PlatformTop : MonoBehaviour
{
    public GameObject player;
    public bool pressure;
       
        void OnTriggerEnter2D (Collider2D col)
        {
            if (col.tag == "Player")
            {
            pressure = true;
            }
        else
        {
            pressure = false;
        }
    }
}

I’m not sure how to go about this, as == didn’t do diddly squat.

Can you do :

pressure = (col.tag == “Player”);

?

  1. initialize it as false to begin with.
  2. you probably need to have an OnTriggerExit handler to reset it to false after OntriggerEnter has set it to true.

I tried that, and it worked! Thanks.

Another option is instead of using a boolean, use an integer and treat it like a boolean, where 0=false and 1=true, then you can do:

value=1-value;

to flip it.

Ah, that actually might solve a lot of problems in future if I get lazy. Ha, I can just make every state with one integer. (Probably not but that is an amusing concept)

Also you can still use a boolean and swap it in a similar manner:

value = !value;

This sets it to Not whatever it currently is.
if value is true it becomes false and vice-versa.

1 Like

Fascinating. You learn something new every day, eh?