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

Bug im new to unity and i was working on a 2d game when i hit the death cube i dont die i stand on top

Discussion in 'Scripting' started by vega20029027, Aug 8, 2023.

  1. vega20029027

    vega20029027

    Joined:
    Aug 4, 2023
    Posts:
    11
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class FinshLine : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.     void Start()
    10.     {
    11.        
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D other)
    21.     {
    22.        if(other.gameObject.CompareTag("Player"))
    23.        {
    24.            SceneManager.LoadScene("level12");
    25.        }
    26.     }
    27. }
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    In your title you talk about a "death cube" but you show code for the "FinishLine". Does that not work either?

    Either way, check if your code runs at all. Place Debug.Log() messages to see if it does. If it does, check if your condition gets entered. If so, check why you dont die despite expecting to die. If not, check why that condition is not true. And so on.
     
  3. Autoface

    Autoface

    Joined:
    Sep 23, 2013
    Posts:
    112
    Try the following:

    Make sure is "Is Trigger" is checked.

    upload_2023-8-9_0-16-9.png

    Then in your scipt:
    Code (CSharp):
    1.  
    2. private void OnTriggerEnter(Collision2D other)
    3.     {
    4.        if(other.gameObject.CompareTag("Player"))
    5.        {
    6.            SceneManager.LoadScene("level12");
    7.        }
    8.     }
    9.  
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,360
    To add to the above comment: OnTriggerEnter only works for 3D colliders. Make sure you use OnTriggerEnter2d if you are using 2D colliders.
     
    Autoface likes this.