- Home /
OnTriggerEnter2D working only on original prefab and not on its copies
TL;DR: OnTriggerEnter2D works perfectly with one instance of a sprite. But it starts to work sporadically if I duplicate said sprite in the scene.
Hi all,
I have a script that should count how many aliens the player has encountered.
To do so I have this set up:
a player sprite prefab with a Box Collider 2D and a Rigid Body 2D attached to it (see image below)
an alien sprite prefab with a Box Collider 2D with Is trigger set to true and a Rigid Body 2D attached to it (see image below)
a script attached to the alien sprite that counts how many times the player passed by an alien (see script)
I'm working on Unity 4.6.3f1 for Mac
The script counts all encounters when I have only one alien prefab in the scene, it counts them no matter how fast the players moves, or how many times per second it passes by the alien. As soon as I duplicate the alien's prefab (Command D), the script counts only some of the encounters. It seems to do it sporadically on all sprites, the original and copies.
I've already tried to:
Delete and recreate Rigid Bodies 2D and Box Colliders 2D on both the alien and the player
Change the collision detection from Discrete to continuos on both sprites
Rename the alien copies
Not use prefabs
I've all layers checked under Edit > Project settings > Physics 2D
Used this code to check if triggers work: Debug.Log("Trigger: " + collider2D.isTrigger);
Restart Unity
Any help is much appreciated, thank you.
The code
using UnityEngine;
using System.Collections;
public class Alien : MonoBehaviour {
private int encounteredAliens;
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag ("Player")) {
encounteredAliens++;
Debug.Log ("encounteredAliens: " + encounteredAliens);
}
}
}
The player's prefab inspector 
The alien prefab inspector 
Does the collider size match the sprite? At least looks tiny in the screenshot. With sporadic, you mean both the aliens are randomly triggering a OnCollisionEnter even though there should be more?
Yes, colliders match the sprite size (colliders are in green in the image below). I tried to increase the size of the collider with no effect.

By sporadically I mean that sometimes OnCollisionEnter is triggered and sometimes is not when the player enters the collider of the two sprites (see link below)
Two aliens screen capture (.gif)
When I have only one sprite OnCollisionEnter is triggered every single time (see link below)
Eehhmm... Is any of the objects negative scaled? How do you move the player? Is the second alien really just a plain duplicate?
Its because when you duplicated the triggers with the script, it meant that each one had its own counter (so say, 5 aliens in the scene would have 5 seperate counters which didn't communicate with each other at all). Each alien only counted its own collisions. Having it on the player means there was just one universal counter, and each collision would add to that counter.
have you changed this?
if (other.CompareTag ("Player")) {
Try to give the aliens a tag with name "Alien" and change this line to:
if (other.CompareTag ("Alien")) {
And activate the trigger on the player collider.
Answer by DiebeImDunkeln · Mar 17, 2015 at 11:13 AM
your counter is on each alien and starts each time by zero. It counts for each alien the collisions with a player. Don't know if this is what you wanted. If you want to count the collision for the player with each alien, you should put the counter on the player.
Your answer