Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Death on collison

Discussion in 'Scripting' started by CoopReed, Nov 17, 2015.

  1. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Hi guys,

    I am pretty new to c# programming and have been playing around with colliders. I have created this script that can destroy my character when they touch the collider. The script is working in a way, when I touch the desired collider it deletes that object and allows me to pass through it, it does not allow me to destroy the object when it touches it.
    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class PlayerDie : MonoBehaviour {
    4.      // Use this for initialization
    5.      void Start () {
    6.    
    7.      }
    8.    
    9.      // Update is called once per frame
    10.      void Update () {
    11.    
    12.    
    13.      }
    14.      void OnCollisionEnter(Collision collision){
    15.          if (collision.gameObject.name == "Spikes") {
    16.              Destroy(collision.gameObject);      
    17.          }
    18.      }
    19. }
     
  2. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1.      void OnCollisionEnter(Collision collision){
    2.          if (collision.gameObject.name == "Spikes") {
    3.              Destroy(gameObject);      
    4.          }
    5.      }
     
  4. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    I've tried that but it dosn't do anything? Anything that I could be doing wrong?
     
  5. aLovedHater

    aLovedHater

    Joined:
    Oct 12, 2014
    Posts:
    16
    if that code works but doesn't do anything, make sure you have istrigger checked in the inspector for the object :)
     
  6. JacksonNull

    JacksonNull

    Joined:
    Nov 3, 2015
    Posts:
    16
    if it's a 2d object
    it needs to be OnCollisionEnter2D
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    what object is the script on? the player or the spikes?
     
  8. CoopReed

    CoopReed

    Joined:
    Nov 17, 2015
    Posts:
    27
    Hey guys I solved this which a bit of research on the API. I enabled is triggered into my collider's that I wanted to die from, then I inserted this code.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DamagedByCollision : MonoBehaviour
    5. {
    6.  
    7.  
    8.     int health = 1;
    9.  
    10.     void OnTriggerEnter2D(Collider2D other){
    11.         Debug.Log("Trigger!");
    12.  
    13.         health--;
    14.  
    15.         if (health <= 0)
    16.         {
    17.             Die();
    18.         }
    19.     }
    20.     void Die()
    21.     {
    22.         Destroy(gameObject);
    23.     }
    24. }
    25.    
    26.  
    This is for anyone else who would like help or had the same problem. I think it is because you cant you the OnCollisionEnter2D function to this kind of thing that I was using it for. Thanks guys!
     
  9. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Good job. You can use oncollisionenter2d to do it as well but you can work that out another time. The original code you posted wasn't using the 2d function
     
    Last edited: Nov 18, 2015
    CoopReed likes this.