- Home /
Problem is not reproducible or outdated
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();
}
}
}
@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
Follow this Question
Related Questions
Trigger area around player. 1 Answer
I have problem destroying my objects 1 Answer
Collider not working... 0 Answers
Two Different Box Colliders Aren't Colliding 1 Answer
walk through an object once 0 Answers