Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by gabrielclapclap · Feb 15, 2019 at 08:04 PM · ontriggerentertag

OnTriggerEnter not firing for objects with the same tag,How to detect multiple instances of the same tag using OnCollisionEnter

Hi all. I'm working on a game where you push a bunch of spheres inside a hole. Each sphere is an instance of a prefab and they all have the same tag and the same script below.

I'm trying to use OnCollisionEnter to detect when each of the balls touch the hole and increment count, but I only get the event called once, twice maximum, even though I have dozens of balls colliding with the hole, and the count is not incremented as it should, hence never entering the if.



Is there a way make sure I capture each of the collisions from each of the balls?

 private void OnTriggerEnter(Collider other)
     {
         Debug.Log("Ball hit: " + other.gameObject.tag);
         if (other.gameObject.tag == "Hole")
         {
             count++;
             Debug.Log("count: " + count);
             if (count > 5)
             {
                 DoSomethingHere();
             }
         }
     }

 

Appreciate your help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by zereda-games · Feb 16, 2019 at 03:35 AM

see if this works?

 [Header("OnTrigger Control:")]
 [Space]

 [Tooltip ("What is the Tag on the Objects we want to find in the scene?")]
 [TextArea(1)]
 [SerializeField]
 private string Tag="Hole";

 [Space]

 [Tooltip ("If we are using as a trigger, is it a trigger enter or trigger exit?")]
 public bool IsTriggerExit=false;

 private void OnTriggerEnter(Collider other)
 {
    if(GameObject.FindObjectsWithTag(Tag).Contains(other.gameObject)){
          count++;
          Debug.Log("count: " + count);
          if (count > 5)
          {
              if(Equals (IsTriggerExit,false)){
                    Debug.Log(string.Format("We made it to {0} with: ", count ) + other.gameObject.name)+"(Enter)");
              }
              DoSomethingHere();
          }
      }
  }

 private void OnTriggerExit(Collider other)
 {
    if(GameObject.FindObjectsWithTag(Tag).Contains(other.gameObject)){
          Debug.Log("count: " + count);
          if (count > 5)
          {
              if(Equals (IsTriggerExit,false)){
                    Debug.Log(string.Format("We made it to {0} with: ", count ) + other.gameObject.name+"(Exit)");
              }
              DoSomethingHere();
          }
      }
  }


Comment
Add comment · Show 3 · 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 gabrielclapclap · Feb 16, 2019 at 05:35 PM 0
Share

Looks interesting and I really appreciate your help. I'm not a very experienced developer though and will test it once I get home in a few hours.

Just to confirm, I have multiple balls, one single hole. This script is inside each of the balls, not the hole. $$anonymous$$y intention was to check if each of the balls has collided with the hole. Is that what is does?

Thanks again!

avatar image zereda-games gabrielclapclap · Feb 17, 2019 at 03:33 AM 1
Share

not quite, with this code, it should be on your hole detecting the balls on Collission aka when the ball is touching the collider.. i'm pretty sure i forgot to put a check in to make sure it downt just jump to 4 right away.... Ohh poop i did too, i have the OnTriggerEnter and OnTriggerExit Backwards.. move line 17 to line 32 to fix the problem. Each time the ball exits the collision aka gets destroyed for example then the point on the Hole adds 1 point otherwise as is, as long as the ball is touching the collider the points will add up to 4 and stop because we have a check to make sure the number doesn't exceed 4. i don;t think what you want is it to be on the ball myself but there are many ways to do things and everyone has their way of doing things.

avatar image zereda-games zereda-games · Feb 17, 2019 at 03:50 AM 0
Share

doing my fix would work too, all you would need to add is when the collisions hits 4 = true then don't spawn any more balls. as the player has used them all up.

so in an Update you would need something like:

 public Transform Parent;
 public GameObject Ball;
 protected int balls=4;
 protected int playerScore;
 private static count=0
 protected const string Save$$anonymous$$ey="SaveData";

 void OnEnable(){
      balls=4;
      playerScore=0;
 }


 void Update(){
      if(Equals(balls,0)){

          InitializeGameOver(playerScore);
          return;
      }

      if(Equals(balls<=0)||Equals(ball>=4))
           if(Imput.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space){
                      instantiate(BallPrefab,Parent.position,Parent.rotation, Parent);
                      balls--;
            }
        }
 }
 void  InitializeGameOver(int score){
             SaveScore(score);
             OnDisable();
 }

 void  SaveScore(int score){
        count++;
        PlayerPrefs.SetInt(Save$$anonymous$$ey+count, score)
  }

  void  OnDisable(){
          //Do something
  }
avatar image
1

Answer by gabrielclapclap · Feb 16, 2019 at 06:00 PM

I got it!!! Such a beginner's mistake...

The count variable was being created inside each of the ball objects, hence it was never incremented as each object had it's own counter.

I created a counter inside a different class and started incrementing it from each of the balls.

BOOM!

Thank 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 zereda-games · Feb 17, 2019 at 03:34 AM 1
Share

ahahha That would do it LOL and upvotes are always appreciated ;)

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

167 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 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 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 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 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 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 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

How to start an animation by entering a trigger 0 Answers

OnTriggerEnter2D how to make an if statement for two tags which are the same 0 Answers

OnTriggerStay not detecting compound colliders? 0 Answers

Check if other.object changes his tag (OnTriggerStay2D) 0 Answers

Nothing is happening with these scripts, why? 0 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