# Instantiated object not tracking collision

I have an issue, and I'm not sure why it's happening. I have a prefab that I've built in a colour-changing game. It has the following:

• Four 2D objects that if they collide with the player (2D collider attached to all), they cause the player to die.
• One 2D object that if the player strikes, it generates a duplicate of itself with the Instantiate function.

All 2D objects are created; The four objects that cause the player to die are being created as normal and continue to cause the player to die. However, the object that instantiates the prefab and increments the score can no longer be collided with. The player appears to pass behind it. I'm pasting my code below. I would really appreciate if anyone can offer advice on this:

void Update () {
if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
{
thing.velocity = Vector2.up * bounciness;
}
scoreText.text = score.ToString();
}
private void OnTriggerEnter2D(Collider2D collision)
{

if (collision.tag == "plusScore")
{
score++;
Destroy(collision.gameObject);
Instantiate(barrier, new Vector2(transform.position.x, transform.position.y + 7f), transform.rotation);
return;
}

if(collision.tag != currentColour)
{
Debug.Log("You DIED!");
score = 0;
}
}


If your player object passes through another object with a collider, it means that the collider is set to Trigger (the 'isTrigger' checkbox is enabled on the component).

When a collider is set to Trigger, no collision happens but the OnTriggerEnter() function is called.

You need to uncheck the 'isTrigger' checkbox, and instead of using

void OnTriggerEnter2D(Collider2D collision)
{
}


you should use

 void OnCollisionEnter2D(Collision2D collision)
{
}

• I'm not sure if I understand your answer. The object is not set to 'isTrigger', if I change the function to a 'OnCollisionEnter2D' function then I can't call the tag. When passing through the object the first time it functions correctly, but the object duplicates itself and when passing through the duplicate, that's when no collision occurs... it's odd, because if the object is instantiating a duplicate of itself then I would have thought it would function identically. – JamiePatt May 12 '18 at 0:32
• I was not aware that OnTriggerEnter() is called on non-trigger colliders so I assumed it was a trigger. In any case, I'm glad you figured out your problem. However, just for future reference, notice that OnTriggerEnter() has a Collider type argument, while OnCollisionEnter() has a Collision type argument. You can get the tag using Collision.collider.tag. – akkwake May 12 '18 at 8:16

I just now solved the problem... although I'm not sure what I did to solve it. I deleted the prefab and recreated it, dragged it into "barrier" field on my player script and it works... might just be a Unity bug. Cheers for your help.