- Home /
 
               Question by 
               TwitoGames · Mar 30, 2020 at 06:53 PM · 
                c#collisioncolliderunity 2dontriggerenter  
              
 
              How can I detect a collision between two clones and delete them both?
When there is two coin clones in the same position and my player goes on top of them, I want it to only add to my coins value by 1 but it does it by how many coins are in the same position. Any help?
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Coin : MonoBehaviour
 {
     [SerializeField]
     private PlayerController player;
     private bool collectCoin;
 
     void Start()
     {
         player = GameObject.Find("Player").GetComponent<PlayerController>();
     }
 
     private void OnTriggerEnter(Collider other) {
         if (other.CompareTag("Player") && collectCoin == false) {
 
             collectCoin = true;
             player.coins++;
             collectCoin = false;
 
             Destroy(this.gameObject);
         }
         //one clone colliding with the other ones in the same position and destroying each other
         if (other.transform.position == gameObject.transform.position) {
             Destroy(other.gameObject);
             Destroy(this.gameObject);
         }
     }
 }
 
               Comment
              
 
               
              Answer by Magso · Mar 30, 2020 at 08:25 PM
Use a bool to detect when it's triggered and use a coroutine to add to the value.
Use this on the coin.
 private void OnTriggerEnter(Collider other) {
     if (other.CompareTag("Player"){
         player.GetComponent<PlayerScript>().addCoin = true;
         player.GetComponent<PlayerScript>().StartCoroutine("CollectCoin");
         Destroy(gameObject);
     }
 }
and this on the player.
 IEnumerator CollectCoin(){
     yield return new WaitForEndOfFrame();
     if(addCoin){
         coins++;
         addCoin = false;
     }
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                