- Home /
Detecting a Null returned by a If statement.
Hello,
I'm trying to get something working, but on the condition that what is stated in the "If" is false.
var BallActive : boolean = false;
function Update () { }
function OnTriggerStay (other : Collider) {
//Check if there's a ball is present in the level box.
if (other.gameObject.GetComponent(Tagger).ball == true) {
BallActive = true;
}
if (!(other.gameObject.GetComponent(Tagger).ball == true)) {
BallActive = false;
}
}
The code should put "BallActive" to true if he is detecting an object with the Tag "ball". But will out "BallActive" to untrue if it does not detect any object tagged with Ball. (All of that, inside the Trigger Box Collider)
Any ideas?
Edit:
It should return something as "Is there a ball present inside the Trigger?"
Cannot use OnTriggerExit, since the ball is destroyed inside the Trigger and is not detected as a ball "Exiting".
Answer by skovacs1 · Nov 03, 2010 at 06:22 PM
You should use OnTriggerEnter and OnTriggerExit to detect entering and exit because OnTriggerStay is only called when a collider is inside the trigger (if one of the two has a Rigidbody or CharacterController) and will not set the variable when the object leaves. To deal with potential enable/instantiation within the trigger, you could use OnTriggerStay in stead of OnTriggerEnter, but beware the overhead this will add. To deal with potential disable/destruction inside the trigger, you would use OnDisable and notify the trigger.
For a single ball.
Ball.js (Attached to your ball)
var activeTrigger : GameObject;
function OnDisable() { if(activeTrigger) { activeTrigger.SendMessage("BallDestroyed"); activeTrigger = null; } }
Trigger.js (Attached to your trigger)
var ballActive : boolean = false;
function OnTriggerStay(other : Collider) { if(other.gameObject.tag == "Ball") { ballActive = true; other.gameObject.GetComponent(Ball).activeTrigger = gameObject; } }
function OnTriggerExit(other : Collider) { if(other.gameObject.tag == "Ball") { ballActive = false; other.gameObject.GetComponent(Ball).activeTrigger = null; } }
function BallDestroyed() { ballActive = false; }
or for multiple balls
Ball.js (Attached to your ball)
var activeTrigger : GameObject;
function OnDisable() { if(activeTrigger) { activeTrigger.SendMessage("BallDestroyed", gameObject); activeTrigger = null; } }
Trigger.js (Attached to your trigger)
var balls : Array = new Array();
function OnTriggerStay(other : Collider) { if(other.gameObject.tag == "Ball") { for(var ball : GameObject in balls) if(ball == other.gameObject) return; other.gameObject.GetComponent(Ball).activeTrigger = gameObject; balls.Add(other.gameObject); } }
function OnTriggerExit(other : Collider) { if(other.gameObject.tag == "Ball") { var i = int = 0; for(; i < balls.Length; i++) if(balls[i] == other.gameObject) break; balls.RemoveAt(i); other.gameObject.GetComponent(Ball).activeTrigger = null; } }
function BallDestroyed(destroyed : GameObject) { var i = int = 0; for(; i < balls.Length; i++) if(balls[i] == destroyed) break; balls.RemoveAt(i); }
Thanks for the tip, but still doesn't work. The ballActive turn True when there's a ball in the region. But doesn't turn False when there's no ball in the region. But I think I just had an idea about why it does not work. OnTriggerStay detect the presence of a collider inside the trigger zone and permit to use this collider (or object rattached to it) so if there's no I can not ask him something similar to "If there is no collider". Correct me if I'm wrong?
On your edit, I cannot use Enter or Exit. Since the region is inside the trigger, so the when it "Leave" is when it's destroyed. Thus, OnTriggerExit doesn't work (already tried). What I try to achieve, is to know if it's present inside a given region or not present.
When you destroy the ball, you set the boolean to false?
So If I get what you're saying, since my ball doesn't "Exit". I'd have to control it through and integer? Adding +1 for each ball that come in game and -1 for each one is destroyed? Was my back up plan, but was hoping for a way that'd be not so tricky.
No. I said if you had multiple balls, you could use an integer.
Answer by Atnas1010 · Nov 03, 2010 at 06:05 PM
if (other.gameObject.GetComponent(Tagger) != null)
if (other.gameObject.GetComponent(Tagger) == null)
Also, if you use camelCase for naming your variables, you are just being nicer to the world :) That means I suggest you name it ballActive
I've tried it into this form : if((other.gameObject.GetComponent(Tagger).ball == true)== null). But it doesn't work. The the boolean doesn't turn on false once the ball disappear.
But have you tried what I posted? Also, saying "== true" is null AFAI$$anonymous$$. And then you almost end up with what I posted. But if you stick with skovacs code, you shouldn't get anymore trouble
Your answer
![](https://koobas.hobune.stream/wayback/20220613081845im_/https://answers.unity.com/themes/thub/images/avi.jpg)