- Home /
 
Click for destroy Prefab, but sometime don't work
I have created an game for Windows 8.1, the gameplay is similar to an ant smasher, but with spaceships. You click on the spaceship for destroy it, but sometime the click don't work, if you click on the spaceship is not destroyed.
this is the part of the script who detect the click on the object:
 void OnMouseDown(){
     Destroy (this.gameObject);
     Instantiate(explosion, this.transform.position, this.transform.rotation);
     AddScore();
     AddCredits();
     }
 
               the object is a prefab and this is the proprety: 
 
Where i went wrong? Thanks for help.
Probably an issue with your spaceship colloder. Try using an encompassing box collider and check if your success rate increases or you can always try the alternate method of using raycast.
Just disable the collider on your spaceships and add a box collider with a size that covers your spaceship completely. Note: This is only to test whether the issue is due to the collider.
I tried to enlarge the box collider at 1 for each axis at 1.5. The game has become much easier, but the problem is solved and I already have ideas to balance the difficulty.
If this has solved your problem please mark it as solved and close it. Have a nice day
Answer by SkandYxyz · Jan 07, 2021 at 12:54 PM
Hi,
i guess you first need to instantiate the prefabs and then you can destroy it with this script.
 public class DestroyPlayAudio : MonoBehaviour
     {
         public AudioSource audioSource;
         public AudioClip clip;
         public float volume=0.5f;
     
         void Update()
         {
              if (Input.GetButtonDown ("0")) 
             {
                 Raycast ray=Camera.main.ScreenPointToRay(Input.mousePosition);
                 Raycasthit hit;
                 if (Physics.Raycast(ray, out hit))
                 {
                     Destroy(hit.collider.gameObject);
                     audioSource.PlayOneShot(clip, volume);
                 }  
             }
         }
     }
 
               Insert your audiofile in the variable slot in the Monobehaviour.
If you want to create games fast without coding learn and use Playmaker or Game Creator.
Your answer