- Home /
How to do collision of external objects on script?
How do I check the collision of two objects on this script?
using UnityEngine;
using System.Collections;
public class collision : MonoBehaviour {
//Objects
public GameObject object1;
public GameObject object2;
}
Answer by meat5000 · Jul 03, 2015 at 04:45 PM
You can not remotely access Collision information from other objects.
This information is generated in the Message Methods
OnCollisionEnter(col : Collision) //Stay and Exit
in the form of argumented Collision info.
The only way to access this information from another object is to have the OnCollisionEnter function involved in the collision fire out the information to somewhere it can be accessed from.
For example (crudely):
Make a List of type collision on an empty gameobject in a script (static access or getcomponent, either) and use this List to capture the col info fired from your OnCollision methods.
You could go one stage further and create a struct which contains Collision info AND also a gameobject (or transform) reference in order to keep it all contained and connected.
I imagine it is essential to give the List elements a TTL (time-to-live) and have it remove (FIFO style) elements older than 2-3 seconds. This will avoid the Game eating your memory.