• 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 /
This question was closed Dec 01, 2017 at 07:55 PM by pako for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by Halok1122 · Dec 01, 2017 at 06:53 PM · destroycollision2dsetactiveontriggerexitontriggerenter2d

OnTriggerEnter2D called twice - doing UFO tutorial

I'm a newbie who just started with Unity, going through the tutorials to try and learn the basics. I just reached the end of the 2D UFO tutorial, and for some reason the OnTriggerEnter2D is calling twice, or some other similar issue causing the count to go up by 2 for each time a pickup is triggered. Both the player and the pickups have a single circle collider 2d attached and a single rigidbody attached, with the player's being dynamic and the pickups being kinematic rigidbody's. There aren't any duplicate pickups in the same place, and each pickup only has a single trigger. I've tried switching from SetActive(false) to destroy, I've tried setting the forces to very small to see if velocity is effecting it, I've replaced it with the OnTriggerExit and that also seems to trigger twice. I'm honestly at a loss for what to do. Only triggering once and then disabling the script wont work cause I need to to go off for each one of the 16 pickups I have placed in the game. None of the other forums with people having questions similar to mine seem to have a solution for this kind of thing, and if I was more experienced I might be able to bypass this, except I'm still on the tutorial. So, 2 questions. 1) Is this supposed to happen or is it a bug? 2) Why is it triggering twice, and how do I solve it? I'm going to post the playerController code down below so it can be read, sorry the formatting's weird, code snippet thing is finicky. I have disabled the only other bit of code that makes the pickups spin, so the problem has to be somewhere in here, as far as I can tell.


TL;DR - OnTriggerEnter is performing its action twice, I've tried to fix it and can't, other forums of a similar problem don't have a solution that will work for mine, can anyone help?


 //playercontroler
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 public class PlayerController : MonoBehaviour 
 {
     public int speed;
     private Rigidbody2D rb2d;
     private int count;
     public Text countText;
     public Text winText;
     public Text debugger;
     private int exitCount;

     void Start()
     {
         rb2d = GetComponent<Rigidbody2D>();
         count = 0;
         countText.text = count.ToString();
         winText.text = "You Win!";
         exitCount = 0;
     }
     
     void FixedUpdate()
     {
         float moveHorizonal =  Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         Vector2 movement = new Vector2(moveHorizonal, moveVertical);
         rb2d.AddForce(movement * speed);
     }

     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.gameObject.CompareTag("PickUp"))
         {
             Destroy(other.gameObject);
             count++;
             countText.text = "Enter Count: " + count.ToString();
         }
     }

     void OnTriggerExit2D(Collider2D other)
     {
         if (other.CompareTag("PickUp"))
         {
             exitCount++;
         debugger.text = "Exit Count: " + exitCount.ToString();
         }
     }
 }
Comment
Add comment · Show 1
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 pako · Dec 01, 2017 at 07:53 PM 0
Share

@Halok1122 I cannot reproduce the problem you report. I downloaded the Tutorial, run it, stepped through its code, and I assure you it's working fine, i.e. OnTriggerEnter2D() is only called once per pickup, with count increasing 1 by 1.

So, it must be something with your setup/implementation.

I suggest you download the Tutorial's complete project, import it in a new Unity project, and then compare it with your own to see what's causing the problem: https://assetstore.unity.com/packages/essentials/tutorial-projects/2d-ufo-tutorial-52143?_ga=2.259943817.2060633278.1511977637-867279775.1480628190

1 Reply

  • Sort: 
avatar image
0

Answer by hughesbenton2023 · Dec 01, 2017 at 07:12 PM

you just meme it dud @pewedidi @unity @jumpinglizard

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

77 People are following this question.

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

Related Questions

Trying to make it so when the enemy collides with the player, the enemy dies, and the player is damaged. 1 Answer

Should I use Destroy() or SetActive(false)? 1 Answer

OnTriggerExit not called on Destroy() 5 Answers

Destroying a gameobject is not working. I tried triggering the colliders but an error comes out. 1 Answer

2D colliders not working with UI? 0 Answers


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