Question by 
               Nicholas-B · Feb 16, 2018 at 07:49 PM · 
                2dcollision2d gametriggerplatformer  
              
 
              Trying to get bool statement to work in OnTriggerStay2D
I have been working on this problem for hours to no avail. Below you will see I have an if/else statement in my OnTriggerStay2D function. The first part works fine, but if my player leaves the trigger object the else statement doesn't read.
Any help is appreciated!
  public LevelManager tLM;
  public bool inCrateLight;
 
     
         void OnCollisionStay2D(Collision2D other)
         {
             if (other.gameObject.tag == "KillPlane")
             {
                 if (inCrateLight)
                 {
                     Debug.Log("Incratelight can't die!");
                 }
                 else if (inCrateLight == false)
                 {
                     Debug.Log("Collision detected");
                     tLM.Respawn();
                 }
             }
         }
     
         void OnTriggerStay2D(Collider2D other)
         {
             if (inCrateLight)
             {
                 if (other.tag == "CrateLight")
                 {
                     Debug.Log("In Crate Light");
                     inCrateLight = true;
     
                 }else{
                     inCrateLight = false;
                 }
             }
         }
 
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Nicholas-B · Feb 16, 2018 at 09:01 PM
I figured out another way to solve this using trigger counters. I have included the code below incase it's useful for anyone else.
     public bool inCrateLight;
 
     public int triggerCount;
 
     // Use this for initialization
     void Start()
     {
         inCrateLight = true;
         triggerCount = 0;
 
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         if(triggerCount == 0 && !inCrateLight)
         {
             Debug.Log("YouDied");
             inCrateLight = true;
         }
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "CrateLight")
         {
             Debug.Log("In Crate Light");
             inCrateLight = true;
             triggerCount++;
 
         }
     }
         
         void OnTriggerExit2D(Collider2D other)
         {
             if (other.tag == "CrateLight")
             {
                 inCrateLight = false;
                 triggerCount--;
             }
         }
     
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                