2
\$\begingroup\$

I need to test from another Script on a different GameObject, whether a particular GameObject is colliding with anything (in particular using trigger).

So, for my purpose, I don't need OnCollisionEnter/OnTriggerEnter events, but I need something like this:

if ( myObject.isOnCollision ) 
   do something

How can I do this?

\$\endgroup\$
3
  • \$\begingroup\$ For clarification: So you have an object with a trigger-collider, and you want to know if a specific other game object (which you already have in a variable) is currently inside the trigger-collider of your current game object? \$\endgroup\$
    – Philipp
    Commented Jul 2, 2018 at 21:54
  • 1
    \$\begingroup\$ You can send an event when a collision happens to another object, or just boolean variable with a getter method and test again it in update. \$\endgroup\$
    – PaulD
    Commented Jul 3, 2018 at 8:15
  • \$\begingroup\$ I've edited your question to try to clarify, since I saw it was getting some close votes for being unclear. Please feel free to revert or improve the edits if I've misunderstood your aim. \$\endgroup\$
    – DMGregory
    Commented Jul 8, 2018 at 14:30

3 Answers 3

3
\$\begingroup\$

If you need a trigger collider to keep track of whether anything is touching it, you can attach a script to it like so:

public class Sensor : MonoBehaviour {
    int _overlaps;

    public bool isOverlapping {
        get {
            return _overlaps > 0;
        }
    }

    // Count how many colliders are overlapping this trigger.
    // If desired, you can filter here by tag, attached components, etc.
    // so that only certain collisions count. Physics layers help too.
    void OnTriggerEnter(Collider other) {
        _overlaps++;
    }

    void OnTriggerExit(Collider other) {
        _overlaps--;
    }
}

Now another script with a reference to public Sensor mySensor; can query if(sensor.isOverlapping) {...} to change behaviour when something is inside this sensor trigger.

Or you can use the various Physics.OverlapBox/OverlapSphere/etc. methods to query a shape of space for collisions.

\$\endgroup\$
0
\$\begingroup\$

You could use tags or layers I guess to make sure it is the right object that is colliding. So I believe (if I understood correctly your problem) that you need in fact OnCollisionEnter/OnTriggerEnter.

private void OnTriggerEnter(Collider collision){ 
    if (collision.gameObject.tag == "TheOtherObjectTag"){
      Debug.Log("Collision using tag");
    }
    if (collision.gameObject.layer == 1 << LayerMask.NameToLayer("TheOtherObjectLayer")){
      Debug.Log("Collision using layer");
    }
    if (GameObject.ReferenceEquals(collision.gameObject,myOtherGameObject)){
      Debug.Log("Collision using an object reference");
    }
}

I didn't try the code but I think the syntax is OK. Tell me if this is not what you need, and please explain further if it is not.

\$\endgroup\$
1
  • \$\begingroup\$ When you aren't sure you understood the question correctly then it is usually better to post a comment and ask for clarification than to guess what could be meant and post an answer which could be misleading. \$\endgroup\$
    – Philipp
    Commented Jul 3, 2018 at 8:05
-1
\$\begingroup\$

I am sure about my code it works and using it.

first you need a void OnTriggerEnter (Collider other) unity builtin function to use enter collider or for exit collider functions create a tag and use exact name

 private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.tag == "Mytag")
    {
        GameObject explosion = Instantiate(Resources.Load("FlareMobile", typeof(GameObject))) as GameObject;
        explosion.transform.position = transform.position;
        Destroy(explosion, 2);
        explosionAud.Play();
        boxend();

        Debug.Log("fireeeee");
        //   Destroy(gameObject);
        Destroy(gameObject, 5);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ How does this help the user check, from an outside script, whether myObject is colliding with something? \$\endgroup\$
    – DMGregory
    Commented Jul 8, 2018 at 14:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .