Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

External object collisions?

Discussion in 'Scripting' started by Deleted User, Nov 30, 2010.

  1. Deleted User

    Deleted User

    Guest

    Hello. I know how to make it so that a function happens when the object that a collision script (like the one below) is attached to collides with another object, but is there a way to make a script that references another object for both objects? For example, the script is attached to object A and it says something like "When object B enters the trigger of an object with the tag "C" object B = destroyed)? Thanks.

    Code (csharp):
    1. function OnTriggerEnter (other : Collider) {
    2.     if (other.gameObject.CompareTag ("Enemy")) {
    3.        Stats.Life -=1;
    4.        Player.enabled = false;
    5.        Light.enabled = false;
    6.        Invoke("LightsOn", Timer);
    7.       };
     
  2. Ezzerland

    Ezzerland

    Joined:
    Jul 1, 2010
    Posts:
    405
    So you want object A to detect when object B collides with object C?
     
  3. Deleted User

    Deleted User

    Guest

  4. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't "listen" for collisions between two other objects but you can script one of the colliding objects to send a message to the third object when the collision happens.
     
  5. meat5000

    meat5000

    Joined:
    Mar 5, 2013
    Posts:
    118
    Sorry for the necro. The method I used here was to simply create a class Collision variable and inside your OnCollisionEnter(Collision col) routine make that variable = col.

    Easy way to expose the information.