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

Question How does OnTriggerEnter2D with Tilemaps work?

Discussion in '2D' started by Paulz_, Oct 18, 2023.

  1. Paulz_

    Paulz_

    Joined:
    Sep 23, 2023
    Posts:
    1
    So I've been trying to make this feature in my game where you could walk up the stairs and then it would change scene (change floor). But I'm not really understanding where I should put the scprit or how I should code this. Here is my current code(this script is all the 3 diffrent Grid sections):
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class StairController : MonoBehaviour
    6. {
    7.     bool Up = false;
    8.     bool Down = false;
    9.     void Start()
    10.     {
    11.         Debug.Log("Hello");
    12.     }
    13.     void OnTriggerEnter2D(Collider2D other)
    14.     {
    15.         if(other.tag=="StairUp")
    16.         {
    17.             Up = true;
    18.             Down = false;
    19.         }
    20.  
    21.         if(other.tag=="StairUp")
    22.         {
    23.             Down = true;
    24.             Up = false;
    25.         }
    26.  
    27.         if(other.tag=="StairConfirm" && Up==true)
    28.         {
    29.             Debug.Log("Up");
    30.         }
    31.  
    32.         if(other.tag=="StairConfirm" && Down==true)
    33.         {
    34.             Debug.Log("Down");
    35.         }
    36.     }
    37. }
    38.  
    So how this works is that I have 3 different tilemaps and 3 different tags. One for Up, Down and Confirm. The stairs are built in a 3 area kinda way. First you either enter a UpStair or a DownStair, and then you come to the confirm area.
    upload_2023-10-18_18-10-38.png
    upload_2023-10-18_18-5-21.png
    The green parts are the diffrent areas, the exclamation mark for walls, and purple is just the floor. So green area above the middle is UpStair, and under that is DownStair. And lastly the green area to the right is Confirm.

    But the problem is that nothing happens in the console.
    It's kind of a mess, maybe you guys have sometips on how to make this less messy? :)
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,354
    So lets start from the beginning, have you attached this script to an object with a collider set as trigger to it? If yes, is the Debug.log in Start showing up in the console?

    Next: If your player (who also has a collider & rigidbody2D) runs into the trigger with the script attached, do the other debug.logs appear? Aka, did you actually tag the objects with the collider + script with the appropriate tags of "StairUp", etc etc?