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

Trigger Not Working

Discussion in 'Scripting' started by Dev_23, Aug 6, 2015.

  1. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EndLevel : MonoBehaviour {
    5.  
    6.     public Transform SpawnPoint;
    7.  
    8.     public int level;
    9.  
    10.  
    11.     void OnTriggerEnter (Collider other){
    12.         if (other.gameObject.tag == "Player") {
    13.  
    14.         gameObject.transform.position = new Vector3(SpawnPoint.position.x, SpawnPoint.position.y);
    15.         level ++;
    16.         }
    17.     }
    18. }
    19.  
    My trigger doesn't get triggered with the OnTriggerEnter function. "Is Trigger" is checked on the box collider of the object, and the other is just a circle collider. Why isn't this working?
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    does something involved in the collision have a rigidbody attached to it? without that the physics engine doesn't know about it
     
  3. Dev_23

    Dev_23

    Joined:
    Oct 4, 2014
    Posts:
    18
    Yes, the player does have a rigidbody2D component attached.
     
  4. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    If this is 2d you need to use OnTriggerEnter2D
     
    mstarr likes this.
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If you are teleporting the player then the transform line needs to be other.gameObject.transform.position =...............

    & your vector3 for where you move it needs to have a z parameter
     
  6. mstarr

    mstarr

    Joined:
    Jun 20, 2018
    Posts:
    135
    This solved my problem. Thank you!