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 cstabile18 · Mar 18, 2014 at 06:40 PM · triggerboxcollider2dontriggerenter2d

Why is this only working for the second object that collides?

I have two game objects that move with user input. Each has a box collider 2d with triggers and a rigid body 2d attached. There is a static game object with tag "Divider" that also has a box collider 2d attached. When a collision of one of the objects occurs with the "Divider," limitX is supposed to then equal false. However, for some reason if object1 collides first the console will log as it is supposed to yet limitX will not switch to false until object2 collides. It doesn't matter which object hits first or second either.

 void OnTriggerEnter2D(Collider2D collider) {
         Debug.Log ("Triggered: " + collider.name); 
 
         if (collider.tag == "Divider") {
             limitX = false;    
         }
 
     }
Comment
Add comment · Show 5
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 MousePods · Mar 18, 2014 at 06:50 PM 0
Share

Did you make absolutely sure the tag is spelled right, capitalized and all? That would seem to be the only problem if you get the Debug.Log.

avatar image edragongaming · Mar 18, 2014 at 06:51 PM 0
Share

This is my coding that works for a game, but i have two separate scripting files for both objects that collide. This is the coding where I instantiate the collision. I hope it helps

{

void OnTriggerEnter(Collider otherObject)

     {
         if (otherObject.tag == "enemy")
             {
                enemy Enemy = (enemy)otherObject.gameObject.GetComponent("enemy");
              }
     }

}

avatar image cstabile18 · Mar 18, 2014 at 07:06 PM 0
Share

Unfortunately I'm still having this issue. I double-checked the spelling and capitalization of the tag in all places and I tried adjusting things based on the code edragonga$$anonymous$$g provided. The thing I find strange is that it does work but only when the second object, whichever that might be, collides with the "Divider." No matter how many times the first object to hit it collides with the "Divider," it is not until the second object hits that it works. I can't figure out why this would be.

avatar image MousePods · Mar 18, 2014 at 07:08 PM 0
Share

Could you package up the script and prefabs and attach it? I can take a look! Its too hard to debugs with so little info =(

avatar image cstabile18 · Mar 18, 2014 at 07:14 PM 0
Share

I am pretty new to Unity and coding in general, and this is just for a basic pong game that I'm trying to make as practice. With that said, here is all the code. I'm sure it's probably not great but as of now I'm just concerned with this particular issue. Hope it's enough. I appreciate the help!

 public class Paddle$$anonymous$$ovement : $$anonymous$$onoBehaviour {
 
     public bool limitX = true;
 
     void Start () {
     
     }
     
     public float paddle$$anonymous$$ovementY = 0.1f;
     public float paddle$$anonymous$$ovementX = 0.1f;
 
     void Update () {
     
         GameObject paddleOne = GameObject.FindGameObjectWithTag ("PaddleOne");
         GameObject paddleTwo = GameObject.FindGameObjectWithTag ("PaddleTwo");
         Vector3 posOne = paddleOne.transform.position;
         Vector3 posTwo = paddleTwo.transform.position;
 
         if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W)) {
             paddleOne.transform.Translate(0, paddle$$anonymous$$ovementY, 0);
 
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.S)) {
             paddleOne.transform.Translate(0, -paddle$$anonymous$$ovementY, 0);
         
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.A)) {
             paddleOne.transform.Translate(-paddle$$anonymous$$ovementX, 0, 0);
             
         }
 
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.UpArrow)) {
             paddleTwo.transform.Translate(0, paddle$$anonymous$$ovementY, 0);
         
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.DownArrow)) {
             paddleTwo.transform.Translate(0, -paddle$$anonymous$$ovementY, 0);         
         
         }
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.RightArrow)) {
             paddleTwo.transform.Translate(paddle$$anonymous$$ovementX, 0, 0);
             
         }
 
         if (limitX == true) {
         
             if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.LeftArrow)) {
                 paddleTwo.transform.Translate(-paddle$$anonymous$$ovementX, 0, 0);
                 
             }
             
             if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.D)) {
                 paddleOne.transform.Translate(paddle$$anonymous$$ovementX, 0, 0);
                 
             }
         
         }
                 
 
     
     }
 
 
     void OnTriggerEnter2D(Collider2D collider) {
         Debug.Log ("Triggered: " + collider.name); 
 
         if (collider.tag == "Divider") {
             limitX = false;    
         }
 
     }
     
 }

1 Reply

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

Answer by MousePods · Mar 18, 2014 at 08:30 PM

Ok, So I hope you don't get mad, but I changed and added a few things.

The way you were doing it wasn't going to work because it will turn off both player's movement controls and other issues.

I made sure to comment everything and explain what I did.

Hope this helps!!!

Dropbox

Comment
Add comment · Show 2 · 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 cstabile18 · Mar 18, 2014 at 10:50 PM 0
Share

Wow, I really appreciate this. While I did really want to make the whole thing on my own, it seems I am not quite there yet. This will certainly give me some new incite on how things work. Thank you!

avatar image MousePods · Mar 18, 2014 at 11:07 PM 0
Share

Np! Good luck learning Unity, it should be fun =]

Also, could you accept the answer? Thanks!

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

22 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

Related Questions

BoxCollider2D, Trigger not working 1 Answer

Trigger only one collider? 1 Answer

transform.position not executed with OnTrigger2D 1 Answer

Trying to call OnTriggerEnter2D once but being called multiple times... 1 Answer

BoxCollider2D not triggering 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