Search Unity

Unity 5.3.5f1 and OnTriggerEnter2D() events?

Discussion in '2D' started by rayrite, Jul 21, 2016.

  1. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    Hello,

    Does anyone have a solution for the following issue?

    I have a player with a BoxCollider2D and a RigidBody2D. I have falling coins with a CircleCollider2D and a RigidBody2D with IsTrigger set to On. I have a DeathZone just below the visible part of the scene with a BoxCollider2D and a Trigger that is supposed to kill the player and destroy the coins when it is touched (falls off the screen). The coins fall due to gravity and the player jumps by the AddForce() function.

    The game works fine when debugging on the laptop, but when I select build or build & run to play it on my Android phone the code inside the OnTriggerEnter2D() is not firing.

    I tried adding a RigidBody2D to the DeathZone and also even changing all the code to use OnCollisionEnter2D without the triggers and I get the same result. It works on the laptop but playing on an Android phone is not working. The player and coins just sit on top of the DeathZone collider when they fall.

    Any help or advice is appreciated.
     
    Last edited: Jul 21, 2016
  2. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    I don't know why I got assigned this strange username and I can't change it. But I am a real person.

    Ray
     
    LiterallyJeff likes this.
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Only suggestion I have is to verify your assumptions. Are you 100% certain the event is not firing at all? Have you tried to output something at the start of the event for your android build? Is it possibly a logical error within the event? Have you tested this on a PC build?
     
  4. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    Hi,
    I did do some message/debugging output tests to verify the event is not getting triggered, but I can try your other suggestion to do a PC build. I can try an iOS build as well. I can reply back with what I find.
     
  5. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    I'm not able to pull my MacBook out to compile the iOS build, however, I did try the PC Standalone and WebGL builds and got the same result as the Android build.
     
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Would you mind sharing your trigger function? If you do, use code tags to paste your code into the forums.
     
  7. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    The unmodified code is below with all the debugging output taken out.

    For reference, here is a video clip of the source code project I am trying to run. I have not heard back from the developer yet. But I'm assuming it worked with an earlier version of Unity at some point.



    The player is also not grabbing coins that it comes in contact with either. But the DeathZone is a long collider bar that extends across the screen off camera at the bottom of the scene.

    This is from the Player.cs file

    Code (CSharp):
    1. //On entering trigger:
    2.     void OnTriggerEnter2D(Collider2D col)
    3.     {
    4.         //If entering dead zone trigger
    5.         if (col.CompareTag("DeathZone"))
    6.         {
    7.             Game.PlaySound(sources[0], soundEffects.deathSFX, 0.35F, 0.8F);     //Play death sound effect with desired volume and pitch (optional);
    8.             StartCoroutine(Respawn());                                          //Start respawn;
    9.             rb2d.velocity = Vector3.zero;                                       //Reset velocity;
    10.             airCoins = 0;                                                       //Null collected in air coins;
    11.             curBlock = null;                                                    //Null current block;
    12.             Game.gamesCount++;                          //Increase games counter (this is static variable, so you can acces it from any script if you need);
    13.             AdsController.Instance.RequireAd();         //Check if ad can be played;
    14.         }
    15.         //If entring coin trigger
    16.         else if (col.CompareTag("Coin"))
    17.         {
    18.             //If player is in air
    19.             if (!isGrounded)
    20.                 airCoins += 1;      //Add air coin (air coins are coins we have collected in air, this coins will be added to coins counter if player will land succesfull.
    21.             else
    22.                 gm.AddCoin(1);      //Add coin;
    23.  
    24.             Game.PlaySound(sources[1], soundEffects.collectSFX);  //Player collect sound effect;
    25.             //Hide coin object;
    26.             col.gameObject.SetActive(false);
    27.         }
    28.         //The third and last trigger in game is score up trigger (part of every block object), when player entering this block we:
    29.         else
    30.         {
    31.             gm.AddScore();                          //Adding score;
    32.             gm.AddCollectedCoins(airCoins);         //Adding coins, collected in air to coins counter;
    33.             airCoins = 0;                           //Null collected in air coins;
    34.         }
    35.     }
     
    Last edited: Jul 21, 2016
  8. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    Here are the game objects. This is the original code. I removed the RigidBody2D component from the DeathZone, this is how the code was originally delivered.


    upload_2016-7-21_16-23-42.png upload_2016-7-21_16-23-51.png
     
  9. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    That all looks fine to me as far as i can tell. You've tried outputting something before the first tag check?

    Only think i would do is try to use "col.gameObject.CompareTag" or "col.gameObject.tag == " for the checks.
     
    rayrite likes this.
  10. rayrite

    rayrite

    Joined:
    Jul 21, 2016
    Posts:
    10
    Thank you very much! Your suggestion at least got me some error output finally! With that I was easily able to diagnose the problem and fix it.. The DeathZone and Coin game objects did not have a tag defined. Defining a tag in the Tag Manager fixed the problem.

    Thanks again!
     
    LiterallyJeff likes this.