Hi guys.
I have the next code:
void OnControllerColliderHit (ControllerColliderHit hit) {
if (hit.transform.tag == "Coin") {
Coin coinDestroyed = (Coin)hit.gameObject;
coinDestroyed.CoinCatched ();
Destroy (hit.gameObject);
GameManager.Instance.CoinCollected ();
}
}
I am trying to cast the “Coin” Object that has been hit in order to call one of its methods (CoinCatched). But I have not found the way to cast the object. Any help will be great.
Assume “Coin” is a script, use hit.gameObject.GetComponent<Coin>();
instead.
After researching a lot I found the best solution for my request: I used OnTriggerEnter inside the Script Component of my Object “Coin”, like this:
void OnTriggerEnter(Collider other) {
if(other.transform.tag == "Player") {
Destroy (gameObject);
GameManager.Instance.CoinCollected ();
}
}
Last step, I set “Is Trigger” onto the Sphere Collider Component of the Coin object. This gave a better control of every object independently. Perfect.