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 stefan1294 · Feb 06, 2014 at 10:56 AM · c#collisiontrigger

How to call OnTriggerEnter once

I'm creating some sort of score system. There's a GUI Text which will show you your score. Every time you hit a triggered, invisible object, your score will raise by one. However, if I use the function "OnTriggerEnter" it gets called at least 14 times. It gets called in the time you are IN the object. I believe OnTriggerStay should function like that.

Anyway, here is my code:

 public static int pScore = 0;
     public GUIText ScoreText;
 
     void OnTriggerEnter2D(Collider2D collider)
     {
         if (collider.gameObject)
         {
             pScore++;
             ScoreText.text = "" + pScore;
             print("Test");
         }
     }

Thanks

Comment
Add comment
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

5 Replies

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

Answer by GenuiTix · Feb 07, 2014 at 09:23 AM

Most likely you should be more precise while checking what cause collision.

In

if (collider.gameObject)

check also for something unique for you main object, as name or tag.

Comment
Add comment · Show 7 · 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
avatar image stefan1294 · Feb 07, 2014 at 09:25 AM 0
Share

Forgot to edit the code. I did that right after I posted this question. It's now looking for a name.

avatar image GenuiTix · Feb 07, 2014 at 01:46 PM 0
Share

Are you sure that your GameObject (which triggers the collision) is not the composition of many colliders?

  • If it is, every collider included will trigger on enter function. You can try execute same code with a very simple GameObject which has one collider.

  • You can add some Debug.Log(collider.name) to see is your object only one that trigger the collision

  • Also you can add OnTriggerExit2D to see are there any other unexpected transitions (e.g. Input $$anonymous$$ey Pressed event is invoked twice per key pressing, $$anonymous$$ey pressed down is invoked once per key pressing)

avatar image stefan1294 · Feb 08, 2014 at 02:07 PM 0
Share

I'm specifically looking for a name. It doesn't react on every colliding object, it reacts on the right object. Here is the code: public static int pScore = 0; public GUIText ScoreText;

     void OnTriggerEnter2D(Collider2D collider)
     {
         if (name == "DetectionLine")
         {
             if (pTouched == 0)
             {
                 pTouched = 1;
                 print("Test");
                 // Do something
             }
         }
     }
 
     void OnTriggerExit2D(Collider2D collider)
     {
         if (pTouched == 1)
         {
             pTouched = 0;
         }
     }

Test will only get printed once, as it should.

p/s Don't worry about the commenting. I comment everything, but in my native language. I removed it in this example

avatar image GenuiTix · Feb 09, 2014 at 11:41 AM 0
Share

Checking the name should be more like

 if (collider.name == "DetectionLine")

Currently you are comparing the name of GameObject to which your script is attached.

avatar image stefan1294 · Feb 09, 2014 at 03:47 PM 0
Share

Yes, however, collider.name returns the name of the object colliding to DetectionLine.

Show more comments
avatar image
0

Answer by davidc · Feb 06, 2014 at 11:05 AM

What if you tried,

 public static int pScore = 0;
 public GUIText ScoreText;
 
 void OnTriggerEnter2D(Collider2D collider)
 {
     if (collider.gameObject)
     {
         pScore++;
         gameObject.collider2D.enabled = false; 
         ScoreText.text = "" + pScore;
         print("Test");
         gameObject.collider2D.enabled = true;
     }
 }

This may work but might not, sorry if it doesnt work. I had a simliar problem where my player would "jump" and "doublejump" at the same time.

I hope it works for you.

Comment
Add comment · Show 1 · 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
avatar image stefan1294 · Feb 07, 2014 at 07:21 AM 0
Share

Unfortunately that didn't work. I "by-passed" it by adding a variable to check if the object is inside the 2d collider, once the object exits the collider, the variable will reset. But I'm not sure if this is the proper way to "fix" this, as OnTriggerEnter should only be called once.

avatar image
0

Answer by kannan21 · Feb 06, 2014 at 11:53 AM

FYI OnTriggerEnter is called only once by default, when ever some thing enter the trigger. So you don't have to worry about calling it only once. Only OnTriggerStay is called repetitively untill the object leaves the trigger.

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
avatar image
0

Answer by asamatterof · Jul 28, 2018 at 05:45 PM

use collision instead of collider

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
avatar image
0

Answer by hendryshaikh2004 · Oct 14, 2020 at 02:18 PM

You can do it like this use bool to check whether it has finished or not if it helps give it alike

 [SerializeField] int ammoCount = 5;
 [SerializeField] AmmoType ammoType;
 bool picked = false;
 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.tag == "Player" && !picked) // I'm using bool to check whether it has trigger 
     {
         print("Gained Ammo");
         FindObjectOfType<Ammo>().IncreaseAmmo(ammoType, ammoCount);
         picked = true;
         Destroy(gameObject);
     }
 }

`

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

24 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

Related Questions

Multiple Cars not working 1 Answer

On Trigger Enter, Collide with object, specific collision 1 Answer

Animation with frozen player ?? 0 Answers

Problem with acing problems with : OnTriggerEnter 0 Answers

Scene Change Collision 2 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