Hey guys,
i have a very simple problem, but my solution is not very efficient.
public class CoinBehaviour : MonoBehaviour {
public GameObject player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter2D(Collider2D collision)
{
Debug.Log("Collision");
if (collision.gameObject.Equals(player))
{
Destroy(gameObject);
}
}
}
The code works perfectly fine. If i run into the Object it gets destroyed. But if i make a prefab out of the gameobject that this script is attached to, the reference to the player GameObject is no longer there.
If i add a lot of this coin prefabs to my scene, i need to make for every coin the reference to the player gameobject and drag this into it in the inspector.
So it seems not very smart to check if the collision.gameObject is the same as the player object (because i have to update this reference for every coin i add).
So my question is how can i detect if the collision object is my player object in a efficient way?
Thanks,
sephyr
