- Home /
 
Trigger won't read my Bool change
Hello there
First off I am making a block game where blocks stack till they hit a trigger I made at the top of the level. The blocks spawn above the trigger so I used a bool that changes after a set time. The trigger should recognize the true statement and do nothing, however if it detects false it ends the game.
So I am confused as to how I can make a trigger check a bool of a object that got instantiated.
Here is what I want my code to do:
When the object gets instantiated, it sees that my bool "isFalling" is true,
after a few seconds(falling time) it will automatically change to false.
If the trigger detects false at all it will call my game over code.
Here is my code for both scripts: Trigger Code: public class Boundary : MonoBehaviour {
     private GameController gameController;
     private ShootableBlock block;
 
     void Start()
     {
         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
 
         if(gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent<GameController>();
         }
     }
 
     void OnTriggerEnter(Collider entity)
     {
         if(entity.CompareTag("Block") == false)
         {
             gameController.GameOver();
         }
     }
 }
 
               Then my Block code(object being instantiated): public class ShootableBlock : MonoBehaviour {
 public int currentHealth;
 public int scoreValue;
 public float fallingTime;
 
 private GameController gameController;
 public bool isFalling;
 void Start()
 {
     StartCoroutine( IsNotFalling());
     GameObject gameControllerObject = GameObject.FindWithTag("GameController");
     if(gameControllerObject != null)
     {
         gameController = gameControllerObject.GetComponent<GameController>();
     }
     if(gameController == null)
     {
         Debug.Log("Can't find 'Game Controller' script");
     }
 }
 public void Damage(int damageAmount)
 {
     currentHealth -= damageAmount;
     if(currentHealth <= 0)
     {
         Destroy(gameObject);
         gameController.AddScore(scoreValue);
     }
 }
 IEnumerator IsNotFalling()
 {
     isFalling = true;
     yield return new WaitForSeconds(fallingTime);
     isFalling = false;
 }
 
               }
Help is much needed thanks to anyone that helps.
Also not sure if I should use OnTriggerStay instead what do you guys think?
Answer by jdean300 · Feb 10, 2017 at 08:34 PM
In your OnTriggerEnter function:
 Block block = entity.gameObject.GetComponent<Block>();
 if (block == null || block.isFalling)
   return;
 else
   do game over stuff
 
              THAN$$anonymous$$ YOU you helped me on two of my questions and both worked!
God bless you!
Your answer
 
             Follow this Question
Related Questions
Alternate between two Audio clips on collision 3 Answers
Animator bool = true on TriggerEnter? 0 Answers
when MainCamera enter a trigger 1 Answer
Prevent one from doing action if one is activated (C# UNITY) 1 Answer
UI button doesn't appear - c# 3 Answers