Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

Player won't get destroyed on collision

Discussion in 'Scripting' started by meierdesigns, Mar 27, 2020.

  1. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    18
    Hey guys,

    I am getting started with coding and my ship is moving, shooting and even destroying objects with its projectiles.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    This is the code I use to destroy the enemies, if the bullet hit's it. It works perfectly:

    Code (CSharp):
    1.  
    2.     public GameObject Enemy;
    3.  
    4.     void OnCollisionEnter(Collision other)
    5.     {
    6.         if (other.gameObject.tag == "bullet")
    7.         {
    8.             // Debug.Log("Wall Hit!");
    9.             Destroy(Enemy);
    10.         }
    11.     }
    Now I wanted to use it on the ship that I'm moving with the arrowkeys and it just doesn't show any collision I logged via script. It doesn't seam to register any collision

    Code (CSharp):
    1.  
    2.     void OnCollisionEnter(Collision other)
    3.     {
    4.         Debug.Log("HEY! I collide with the wall");
    5.  
    6.         if (other.gameObject.tag == "Wall")
    7.         {
    8.             Debug.Log("HEY!!");
    9.             Destroy(this);
    10.                         Destroy(Spaceship);
    11.         }
    12.     }
    The walls have the tag "Wall" and I tried to destroy the ship with the Destroy(this); as well as the Destroy(Spaceship); command but the ship doesn't even react to void OnCollisionEnter(Collision other). The Debug.Log "HEY! I collide with the wall" won't show up in the console.

    Any idea how to find the problem?

    Thank you for your help.

    Best regards,
    Lance
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    5,510
    To receive collision events you need a Rigidbody where the script is located. Do you have this?
     
  3. meierdesigns

    meierdesigns

    Joined:
    Aug 3, 2016
    Posts:
    18
    That did the trick :D Thank you!
     
    Kurt-Dekker likes this.
unityunity