Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How Do I Swap This Boolean?

Discussion in '2D' started by Pixelkitty, Jan 12, 2015.

  1. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    I've got a problem... I can't figure out how to make this boolean false whenever I am not triggering!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlatformTop : MonoBehaviour
    5. {
    6.     public GameObject player;
    7.     public bool pressure;
    8.        
    9.         void OnTriggerEnter2D (Collider2D col)
    10.         {
    11.             if (col.tag == "Player")
    12.             {
    13.             pressure = true;
    14.             }
    15.         else
    16.         {
    17.             pressure = false;
    18.         }
    19.     }
    20. }
    I'm not sure how to go about this, as == didn't do diddly squat.
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    Can you do :

    pressure = (col.tag == "Player");

    ?
     
  3. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    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.
     
  4. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    I tried that, and it worked! Thanks.
     
  5. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    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.
     
  6. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    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)
     
  7. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    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.
     
    theANMATOR2b likes this.
  8. Pixelkitty

    Pixelkitty

    Joined:
    Dec 30, 2014
    Posts:
    27
    Fascinating. You learn something new every day, eh?