Вы были очень близки с вашим первоначальным решением, но, возможно, вы неправильно понимаете, что на самом деле здесь происходит.Поэтому я задокументировал свое решение, чтобы показать разницу.
/* The following script is called when a Rigidbody detects a collider that is
* is set to be a trigger. It then passes that collider as a parameter, to this
* function.
*/
void OnTriggerEnter (Collider other)
{
// So here we have the other / coin collider
// If the gameObject that the collider belongs to has a tag of coin
if (other.gameObject.CompareTag("Coin"))
{
// Set the gameObject that the collider belongs to as SetActive(false)
other.gameObject.SetActive(false);
}
}
Если вы хотите, чтобы монета была удалена со сцены, поскольку вы не ожидаете, что она когда-либо будет реактивирована, вы можете изменить этот код следующим образом:
void OnTriggerEnter (Collider other)
{
if (other.gameObject.CompareTag("Coin"))
{
// Removes the coin from your scene instead
Destroy(other.gameObject);
}
}