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

New Area Loads When Colliding With Any Object

Discussion in '2D' started by Cobble, Nov 3, 2016.

  1. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Hi! I made a script to load a new scene when colliding with the player. However, the object that has this script moves, and the scene loads when said object touches anything with a box collider. It's only supposed to load a new scene when touching the Player, but it's not working properly.

    Here's my script

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class LoadNewArea : MonoBehaviour {
    6.  
    7. public string loadLevel;
    8.  
    9. void OnTriggerEnter2D(Collider2D other) {
    10. if (other.gameObject.name == "Player")
    11. Debug.LogError("Collisiondetected,TryingtoLoad");
    12.  
    13. {
    14. SceneManager.LoadScene(loadLevel);
    15. }
    16. }
    17. }
     
    Last edited: Nov 4, 2016
  2. Hyblademin

    Hyblademin

    Joined:
    Oct 14, 2013
    Posts:
    725
    Looks like your braces need tightening.

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other) {
    2.  
    3.     if (other.gameObject.name == "Player")  //Note that you can just use Collider2D.name here if you want
    4.     {
    5.         Debug.LogError("Collisiondetected,TryingtoLoad");
    6.         SceneManager.LoadScene(loadLevel);
    7.     }
    8.  
    9. }
     
    LiterallyJeff likes this.
  3. Cobble

    Cobble

    Joined:
    May 9, 2015
    Posts:
    33
    Thank you so much! It works now!