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 alizan · May 08, 2013 at 12:36 PM · c#ontriggerenter

OnTriggerEnter doesn't work as it should? (c#)

Hi everyone, I am facing a problem with OnTriggerEnter.

Why does OnTriggerEnter work at start, but after it has runned as it should and you change variables for the conditions, it doesn't work? The methods that sets/changes the variables are the same for the start condition, and when event is triggered

I have debugged to check that the variables are actually changed to what we want, and that happens correctly. I am checking for collision detection for up to 4 conditions, four different objects colliding with the Collider. Basically you have four version of the code below, with four variable for each(activeCell2, activeBodyPart2, danceMove2Completed, currentDanceMove2 etc)

The variables are changed correctly, but they aren't beeing checked in OnTriggerEnter, even though they should have.

Thank you and apologize for a little messy code.

 void OnTriggerEnter (Collider cell)
     {
         
         if (cell.name == "cellID " + activeCell && this.name == activeBodyPart && !danceMoveCompleted) {
             /**< if the active cell and  our active bodypart collides, and dancemoves(so we can set currentMove to next dancemove when a dancemove is complted)
              * not completed and a dance is not completed*/  
             
             cell.renderer.enabled = false; // after every collision, make cells invisible/dissapear
             
             if (counter < currentDanceMove.Length - 1) {
                 drawFirstInstruction (currentDanceMove, counter);
             }
             if (counter < currentDanceMove.Length - 2) {
                 drawSecondInstruction (currentDanceMove, counter);
             }
             if (counter < currentDanceMove.Length - 3) {
                 drawThirdInstruction (currentDanceMove, counter);
             }
             Debug.Log (activeBodyPart + "collided with " + cell.name + " Counter: " + counter);
             
             if (counter < currentDanceMove.Length - 1) {    /**< increment counter by one while counter is less than second last element of the array*/  
                 counter++;
             }
             
             if (activeCell == currentDanceMove [currentDanceMove.Length - 1]) { // if dance move is completed and dance is not completed
                 danceMoveCompleted = true;    
                 checkMoves (numOfabp); 
                 danceMoveCompleted = false;
                 danceMoveCompleted = true;
                 
             } // end if
             
             activeCell = currentDanceMove [counter]; // active cell is set to the incrementing counter variable in currentDanceMove array for every collision
 
         } // end first if
         
Comment
Add comment · Show 6
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 Owen-Reynolds · May 08, 2013 at 12:50 PM 0
Share

Print your vars as you enter the OnTrigger. Probably a mispelling or extra letters. If it does print, you know the OnTrigger is fine, and it's your code. If it doesn't print, then you know OnTriggerEnter isn't firing (maybe you never fully leave the trigger?)

1st line OnTrigger: Debug.Log("name="+cell.name+" wantC="+activeCell);.

If needed, same with activeBodyPart and this.name.

avatar image alizan · May 08, 2013 at 01:02 PM 0
Share

You are right in that, I had stated a print inside ontriggerenter earlier, and this did run. Problem is that when the variables in the conditions for the ontriggerenter are changed, that block doesn't run anymore, even when the conditions are met.

avatar image Justei · May 08, 2013 at 01:22 PM 0
Share

Are you sure you are leaving the trigger fully? Try running: Debug.Log("Trigger: " + cell.name);

Just after: void OnTriggerEnter (Collider cell) {

To see if you actually go back inside? Then, after that, if it does, try debugging by check the values one by one, so try each one of the conditions by itself, until you find the one that stops it from firing :).

avatar image alizan · May 08, 2013 at 01:57 PM 0
Share

Thank you for reply, I tried that and it did go back inside. So I tried to debug this statement: At first activeCell is 54, this is changed to 55 and when the leftElbowCube object collides with activeCell 55 it actually prints out: "BodyPart collided: leftElbowCube" "activeBodyPart: leftHandCube" "activeCell: 54"

Which is strange, as the activeBodyPart should have been leftElbowCube, and activeCell should have been 55

 void OnTriggerEnter (Collider cell)
     {
         if (cell.name == "cellID " + activeCell && this.name=="leftElbowCube"  ) {
             
             Debug.Log ("Bodypart collided: " + this.name);
             Debug.Log ("Activebodypart: " + activeBodyPart);
             Debug.Log ("ActiveCell: " + activeCell);
             
         }
avatar image Owen-Reynolds · May 08, 2013 at 01:58 PM 0
Share

The conditions are probably not being met -- an extra space, etc... . Try testing each one directly, for example:

if(cell.name == "cellID " + activeCell) Debug.Log("same cell"); else Debug.Log("diff cell");

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by alizan · May 08, 2013 at 03:25 PM

The variables inside OnTriggerEnter doesn't seem to change, even though they are changed outside OnTriggerEnter which the debug statements are showing.

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 Sonaten · May 09, 2013 at 09:51 AM 0
Share

You shouldn't use the answer function to comment.

avatar image
0

Answer by Owen-Reynolds · May 09, 2013 at 02:40 PM

RE: comment about solving using static vars

So your trigger script has got string activeCell; as a global, resulting in 4 copies of activeCell (one for each trigger)? And instead, there should be a single copy they all share? Static vars are one way to do that, but they almost always cause problems later on (for example, you want to add a 2nd dancer.)

Instead of statics, can create one a "I'm the dancer" script, with all the common data and routines for one dancer: Ex:

 // dancerScript, for one dancing thing
 public string activeCell;
 public void drawFirstInstructions() {...}

Then have the triggers all refer to that dancer:

 // triggerScript:
 public dancerScript DS; // drag object w/dancerScript here

 void OnTriggerEnter(...) {
   if(DS.activeCell == ... ) DS.drawFirstInstruction();
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 alizan · May 09, 2013 at 02:59 PM 0
Share

Thank you Owen,

I will keep that in $$anonymous$$d.

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

14 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

OnTriggerEnter triggering twice on one collider? 5 Answers

AI Attack Not Working! W/Video 1 Answer

Strange OnTriggerEnter/Exit() Behavior 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