- Home /
Trigger Trigger Script When Collider Enters
I'm trying to teleport the player when the player enters a certain trigger zone. I created the trigger zone by adding a box collider on to an empty gameobject. I put the Teleport script on the trigger object (the empty gameobject with the trigger), but when I used the OnTriggerEnter function inside the Teleport script, it seems to not run. I was wondering if there was a way to activate the script. Here is my code for my teleport script:
public class Teleport : MonoBehaviour {
public int number;
public Transform player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
player = go.transform;
}
void OnTriggerEnter(Collider2D par) //This part doesn't work
{
if (par.tag == "Player") {
Debug.Log ("Worked");
switch (number) {
case 0:
player.position = new Vector3 (10.0f, 0.0f, 0.0f);
break;
case 1:
player.position = new Vector3 (-1.05f, -4.4f, 0.0f);
break;
}
}
}
}
Also can you guys check if my switch system would work if I had two separate trigger zones if I selected two different numbers for both. For example, one trigger zones number is set to 0, and another one in a different location is set to 1.
OnTriggerEnter() works with 3D colliders and rigidbodies, but you're passing a Collider2D, which would ins$$anonymous$$d work with OnTriggerEnter2D. Is your game 2D or 3D?
Then you need to use OnTriggerEnter2D(Collider2D other)...
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html
Does the Player object have a Collider2D and a non-static Rigidbody2D? Does the teleporter object's Collider2D have IsTrigger enabled in the inspector?
$$anonymous$$y Player has a Collider2D and Rigidbody. isTrigger is enabled for the teleporter as well.
Answer by Yoshinator2 · May 11, 2017 at 01:51 PM
Your problem is simpler than you think. You are using:
void OnTriggerEnter(Collider2D par)
If your game is 2D, you need to use:
void OnTriggerEnter2D(Collider2D par)
Please accept this answer if this solved your problem. If not, feel free to ask any more questions!
Answer by JMF1901 · May 12, 2017 at 12:15 PM
Appreciated!
Here is my improved code that will teleport the gameobject tagged "Player" when it enters the trigger zone. The location of teleportation can be modified in the inspector for each teleporter. Remember this code will only work in 2D ... I repeat 2D! You must put OnTriggerEnter2D for a 2D Program.
using UnityEngine;
using System.Collections;
public class Teleport : MonoBehaviour {
public float x;
public float y;
public Transform player;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
player = go.transform;
}
void OnTriggerEnter2D(Collider2D par)
{
if (par.tag == "Player") {
Debug.Log ("worked");
player.position = new Vector3(x,y,0.0f);
}
}
}
This will only work if the 2D collider of the trigger zone has "istrigger" enabled, and the player has both a Collider and Rigidbody. Thank You again for those who helped @Yoshinator2 @ritoban @tanoshimi
Answer by ritoban · May 11, 2017 at 02:04 PM
Are you certain that the OnTriggerEnter function is being called? Common problems: - The Collider has not been set as a trigger - You should be using OnTriggerEnter2D instead (depending on your project) - Your player is not tagged correctly - etc.
On a side not, you really should not hard-code in the values for where to teleport. Keep them as public fields which take in either a transform for another gameObject or Vector3s with Gizmos displaying their actual position in the scene view.
Answer by Shubhangi25 · May 14, 2017 at 04:40 PM
Check by debugging that your OnTriggerEnter has been called or not. If its not called than you check that both objects the one which supposed to be the trigger and the one which is supposed to enter in trigger should have box collider on them. At least one of the have a rigidbody on it to make trigger work and calling this function. Also if you working on a 2D game and the box collider is 2d you need to call OnTriggerEnter2D.
Your answer
![](https://koobas.hobune.stream/wayback/20220612121733im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger Sound more than Once 0 Answers
How to disable player gravity 2 Answers
Enable Trigger On Trigger 1 Answer
transpoter not working! 1 Answer