Collision between objects?

I'm currently doing collisions using the following code:

function OnTriggerEnter (boxCollider : Collider) {
    if(boxCollider.collider.name == "Box"){
        hittingVar = true;
    }
}

This is added to an object (A) and detects collisions with another object (B). However now what I want to do is to look at collisions between A and B from a third object (C). This needs to be extremely precise and up to date so basically I'm wondering if there is a piece of code I could use to say "If A intersects B then do this" that I could apply to C. I don't want to simply use variables and get them from another script because as I say it needs to be extremely up to date and there seems to be a small amount of lag when i do that :).

Thanks in advance!

A couple of options.

  1. You can set a reference to your third object

    public subject:ComponentC
    
      function OnTriggerEnter (boxCollider : Collider) {
     if(boxCollider.collider.name == "Box"){
        hittingVar = true;
        subject.NotifyCollision(this.gameObject, boxCollider.gameObject)
    }
    
    

    }

  2. You can use send message

    function OnTriggerEnter (boxCollider : Collider) {
         if(boxCollider.collider.name == "Box"){
            hittingVar = true;
            GameObject.FindWithTag("Subject").SendMessage("NotifyCollision");
        }
    
    

Setting a variable directly on C should be faster than any of those method, IMHO, but ugly and prone to errors.

There should be no lag unless you search object C within your OnTriggerEnter() function everytime. Instead, search your object C in Awake() or Start() once (or directly assign it in the Inspector), and then refer to that variable. Then modify your script to call one of C's functions:

var objC : ObjectCScript;

function Awake () {
    objC = Find("ObjectC's name").GetComponent(ObjectCScript);
}

function OnTriggerEnter (boxCollider : Collider) {
    if(boxCollider.collider.name == "Box"){
        objC.DetectedCollision();
    }
}