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. Dismiss Notice

Infinite runner Kill wall script not doing what it is made to do.

Discussion in 'Scripting' started by ExplosiveBear, Feb 11, 2015.

  1. ExplosiveBear

    ExplosiveBear

    Joined:
    Sep 21, 2014
    Posts:
    2
    So as of right now im working on a prototype for an infinite runner as my first game. and for some odd reason the script i had made for the kill walls will not kill the player. now i dont have alot of experience in coding at all and im simply following the unity live training video
    -----------------------------------------------------------
    using UnityEngine;
    using System.Collections;

    public class DestroyerScript : MonoBehaviour {

    void OnTrigggerEnter2D (Collider2D other)
    {
    if (other.tag == "Player")
    {
    Debug.Break ();
    return;
    }

    if (other.gameObject.transform.parent)
    {
    Destroy (other.gameObject.transform.parent.gameObject);
    }
    else
    {
    Destroy (other.gameObject);
    }
    }
    }
    ---------------------------------------------------------------------------------------------------------------------------
    im not too sure on why it is doing this. any help to correct this problem will be highly appreciated.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    First of all, please use code tags.

    If you want something to be killed by collisions, you should be using OnCollisionEnter2D, rather than OnTriggerEnter2D.

    This script, placed on your player gameObject, should work:

    Code (CSharp):
    1.  
    2. [RequireComponent(typeof(Rigidbody2D))]
    3. public class DestroyOnCollision : MonoBehaviour {
    4.  
    5.     void OnCollisionEnter2D(Collider2D other) {
    6.    
    7.           Destroy(gameObject);
    8.  
    9.     }
    10.  
    11. }
     
  3. ExplosiveBear

    ExplosiveBear

    Joined:
    Sep 21, 2014
    Posts:
    2
    upon closer inspection of this. i think i should learn the basic of C# instead of just following along with a tutorial :<
     
    BenZed likes this.