Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by JMF1901 · May 10, 2017 at 09:10 PM · scripting problemtrigger

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tanoshimi · May 10, 2017 at 09:11 PM 1
Share

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?

avatar image JMF1901 tanoshimi · May 10, 2017 at 09:13 PM 0
Share

Sorry but my game is 2D.

avatar image tanoshimi JMF1901 · May 10, 2017 at 10:12 PM 1
Share

Then you need to use OnTriggerEnter2D(Collider2D other)...

https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html

avatar image Commoble · May 10, 2017 at 10:23 PM 0
Share

Does the Player object have a Collider2D and a non-static Rigidbody2D? Does the teleporter object's Collider2D have IsTrigger enabled in the inspector?

avatar image JMF1901 Commoble · May 11, 2017 at 08:15 PM 0
Share

$$anonymous$$y Player has a Collider2D and Rigidbody. isTrigger is enabled for the teleporter as well.

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Why don't I get 20 poits in my trigger function? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges