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

OnTriggerEnter2D not working

Discussion in '2D' started by John_U, Apr 11, 2020.

  1. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Hi,

    I have read quite a number of posts on this issue, however my code is still not working. When I walk into a bush which I have selected as having a box collider 2d with no trigger, it works fine. The bush stops my player and I cannot walk through it. However, when I have a box collider2d as a trigger, what is meant to happen is the next scene is meant to load but it doesn't. I was following a tutorial however I cannot work out what is wrong, any help greatly appreciated. Screenshots are below.

    Code for script LoadNewArea

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

    public class LoadNewArea : MonoBehaviour
    {

    public string leveltoload;
    public string exitPoint;
    private PlayerMovement_ thePlayer;

    // Start is called before the first frame update
    void Start()
    {
    thePlayer = FindObjectOfType<PlayerMovement_>();
    }

    private void OnTriggerEnter2d(Collider2D collision)
    {
    Debug.Log("triggered");
    if (collision.gameObject.name == "Player")
    {
    Debug.Log("triggered");
    SceneManager.LoadScene("InsideShop");
    thePlayer.startPoint = exitPoint;
    }

    }
    }

    As you can see above, I also tried replacing the leveltoload string with the exact name of the scene, "InsideShop" but it still didn't work. In the tutorial the tutor said dont need an exitpoint at the moment, and the tutors code works fine in the youtube video.

    This is the object with the trigger. Basically the player walks into a building and the scene should change to inside of building. Both scenes have been created, so its not like its trying to go to a scene which doesn't exist.
    upload_2020-4-12_8-16-32.png

    Player object below
    upload_2020-4-12_8-17-19.png

    Thankyou for your help
     
  2. RobeKey

    RobeKey

    Joined:
    Oct 3, 2015
    Posts:
    15
    Hi, John_U.

    Can you share the tutorial you are following?

    Also, maybe the tutor offers a download of the completed project files?

    Cheers.
     
  3. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Hi RobeKey, Its called awesome tuts and I just subbed, so will download the files shortly. Thankyou for advice.
     
    RobeKey likes this.
  4. Pixitales

    Pixitales

    Joined:
    Oct 24, 2018
    Posts:
    226
    You wrote private void OnTriggerEnter2d(Collider2D collision), its actually spelled OnTriggerEnter2D
     
    John_U likes this.
  5. John_U

    John_U

    Joined:
    Feb 20, 2019
    Posts:
    9
    Thankyou so much Pixitales,

    It is now working. I have spent almost a day trying to fix this issue. I will be more aware of the syntax used in future.

    Appreciated.
     
    Pixitales likes this.
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Glad you got it working. Note for the future that you can use code tags when posting code to make it much easier for others to read it over plain text.
     
    Pixitales and RobeKey like this.