Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Piotrone · Mar 15, 2015 at 11:50 PM · ontriggerentertriggers

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:

  1. a player sprite prefab with a Box Collider 2D and a Rigid Body 2D attached to it (see image below)

  2. 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)

  3. a script attached to the alien sprite that counts how many times the player passed by an alien (see script)

  4. 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:

  1. Delete and recreate Rigid Bodies 2D and Box Colliders 2D on both the alien and the player

  2. Change the collision detection from Discrete to continuos on both sprites

  3. Rename the alien copies

  4. Not use prefabs

  5. I've all layers checked under Edit > Project settings > Physics 2D

  6. Used this code to check if triggers work: Debug.Log("Trigger: " + collider2D.isTrigger);

  7. 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 alt text

The alien prefab inspector alt text

screen-shot-2015-03-15-at-72624-pm.png (107.9 kB)
screen-shot-2015-03-15-at-73136-pm.png (95.4 kB)
Comment
Add comment · Show 19
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 hexagonius · Mar 16, 2015 at 06:06 PM 0
Share

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?

avatar image Piotrone · Mar 16, 2015 at 06:57 PM 0
Share

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.

alt text

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)

One alien screen capture (.gif)

screen-shot-2015-03-16-at-72838-pm.png (27.2 kB)
avatar image hexagonius · Mar 16, 2015 at 09:43 PM 0
Share

Eehhmm... Is any of the objects negative scaled? How do you move the player? Is the second alien really just a plain duplicate?

avatar image Okido · Mar 17, 2015 at 12:22 PM 1
Share

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.

avatar image DiebeImDunkeln · Mar 17, 2015 at 02:34 PM 1
Share

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.

Show more comments

1 Reply

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

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Please HELP: ¿How can i made a drag and drop quiz?, i got some problems whit my scripts, like connecting the drag and drop function whit the buttons 0 Answers

Play animation OnTriggerEnter (C#) 1 Answer

On Trigger enters being called a frame late 1 Answer

Build and Run is breaking my triggers! 1 Answer

Can I make A trigger, Trigger the OnTriggerEnter 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