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".
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;
}
}