Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

OnTriggerEnter / OnGUI not working in build

Discussion in 'Editor & General Support' started by ehagerup, Mar 17, 2009.

  1. ehagerup

    ehagerup

    Joined:
    Mar 17, 2009
    Posts:
    4
    Good day!

    I am quite new to Unity, but I already have a problem in my early build.. Inside the editor, the OnTriggerEnter works perfectly from a Capsule Collider. However, when I do a Build Run, the game doesn't seem to register the triggers anymore.

    Any idea why this is happening? This is very bad timing for this to happen, so any tips would be very welcome! :)

    Eivind

    EDIT: I've used OnTriggerEnter, not OnCollisionEnter.

    EDIT2: I've also discovered that OnGUI is not working in the build.. We're using both C# and JavaScript in the project. Could this cause these problems?
     
  2. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Hmm, not sure if you used a collider or if you marked the collider as trigger. If the latter one, you need OnTriggerEnter instead of OnCollisionEnter. Was it that?
     
  3. ehagerup

    ehagerup

    Joined:
    Mar 17, 2009
    Posts:
    4
    Aw, sorry, that was just a typo. I am of course using a OnTriggerEnter. It works perfectly in Unity, but not in the build..

    Any ideas?
     
  4. ehagerup

    ehagerup

    Joined:
    Mar 17, 2009
    Posts:
    4
    This is still a huge problem for me, as I haven't found any solution yet..


    The code snippet is really simple:
    function OnTriggerEnter (hit : Collider) {

    if(hit.gameObject.tag == "Player"){

    // do something
    }
    }

    function OnTriggerExit (hit : Collider) {
    // do something else
    }

    Not really any room for mistakes there, is there? As I said, it all works perfectly inside the Editor, it's only the build that makes the problems..
     
  5. rh115

    rh115

    Joined:
    Nov 19, 2008
    Posts:
    81
    What I ever met is the trigger is too small and when the player passes it, Unity does not respond to the trigger.

    Make the trigger bigger or let the player goes through the trigger slower.
     
  6. ehagerup

    ehagerup

    Joined:
    Mar 17, 2009
    Posts:
    4
    No, that shouldn't be it. Actually the triggers are really large, but thank you for replying :)

    Also, now I just discovered that the OnGUI function (GUI.Box / GUI.Label) doesn't work in the build either .. Something has to be really wrong here somewhere. :x
     
  7. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Have you checked the player log file to see if there are any errors logged?
     
  8. AngryAnt

    AngryAnt

    Keyboard Operator Moderator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Does your script for some reason import UnityEditor or did you place it in the /Assets/Editor folder? In either case, unity will not include that script in the build.
     
  9. ivanmoen

    ivanmoen

    Joined:
    Dec 26, 2008
    Posts:
    102
    I have the same problem when building to the iPhone:

    function OnTriggerStay (collision:Collider){
    if(collision.gameObject.tag == “myBox”){
    // this code don’t run on build <--
    }
    }

    If I assign the gameObject to a variable like this it works:

    var myBox:GameObject; // here I drop the gameobject tag’ed “myBox” in the editor

    function OnTriggerStay (collision:Collider){
    if(collision.gameObject == myBox){
    // this code runs <--
    }
    }

    Can’t I use tags when building to the iPhone?
     
  10. dalo

    dalo

    Joined:
    Jul 2, 2009
    Posts:
    4
    Has anyone solved this issue? my OnTriggerEnter does not even get called on the iPhone but works perfectly on the mac.......frustrating.
     
  11. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    this is strange , cause on a game i am doing i use OnTriggerEnter quite a bit with tag and everything seem to work perfectly as supposed form unity to iphone or whatever build

    and on the last beta6

    using something in this fashion >>

    void OnTriggerEnter(Collider obj)
    {
    if(obj.tag == "something")
    //Do something
    }

    and well for now i don't have any issue with that in my build, at least for the detection
     
  12. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There doesn't seem to be anything about this in the bug database. Please can you file a bug report for it (menu: Help > Report A Problem).
     
  13. YorkChan

    YorkChan

    Joined:
    Feb 16, 2017
    Posts:
    1
    I also have this problem, have you solved that?
     
  14. Daria2B

    Daria2B

    Joined:
    May 15, 2016
    Posts:
    4
    I don't know if anyone is still looking for a solution, but searching around the web and analysing what kind of changes I made right before it stopped working in my game I found a solution.

    In my case the earlier builds actually were working fine. I have falling objects from the top of the screen and as they collide with the ground (tagged as "Ground") they should be destroyed.

    The problem was that I removed a tag at some point (some random tag I was not using, not the "Ground" tag), and the build stopped recognizing the "Ground" tag, even though it still existed.

    I created a new tag and rewritten my script to use that new tag - works perfectly!

    Hope this helps
     
  15. mudabbirali92

    mudabbirali92

    Joined:
    Jan 25, 2016
    Posts:
    4
    i have some issue while building the game on PC that in my "Roll a ball assignment" the function "OnTriggerEnter" doesn't work but works fine while staying in game scene. here is the code:

    void OnTriggerEnter (Collider other)
    {
    if (other.gameObject.CompareTag ("Pick ups"))
    {
    other.gameObject.SetActive (false);
    count= count + 1;
    TextCounts ();
    }
    }
     
  16. thislop1

    thislop1

    Joined:
    Jun 6, 2014
    Posts:
    9
    Heyo, just ran into this problem in 2018 and fixed it!

    Specifically, what I found was that if you have deleted a layer for whatever reason, Unity doesn't update the array of layers to account for this change until you restart Unity. As a result the indexes weren't lining up and I was getting build only OnTrigger Errors.

    Restarted Unity and checked the tags and built, all was good! Hope this helps someone!
     
    AlekseyEgorov and thegreatkoala like this.
  17. Gregory_HN

    Gregory_HN

    Joined:
    Sep 28, 2017
    Posts:
    4
    I had the same problem, but your solution is worked for me!

    reproduce: remove unnecessary tag; now Inspector says "removed" but Unity stopped recognizing other tags.
    solution: shutdown Unity and launch again. Now "removed" tag is gone, and other tags will be work.

    Please, fix this bug!
     
    Last edited: Nov 24, 2018
  18. thegreatkoala

    thegreatkoala

    Joined:
    Jun 30, 2017
    Posts:
    11
    Thank you thislop!, I had the same issue and your post fixed it :)
     
  19. gtasyurek

    gtasyurek

    Joined:
    Nov 30, 2019
    Posts:
    1
    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.tag == "CollectableItem")
    4.         {
    5.             Destroy(other.gameObject);
    6.             counter++;
    7.             Debug.Log(counter);
    8.             txtCounter.text = counter.ToString() + "/7";
    9.  
    10.             if (counter == CollectableItemCount)
    11.             {
    12.                 txtGameOver.transform.gameObject.SetActive(true);
    13.                 Debug.Log("game over");
    14.             }
    15.         }
    16.     }
    to
    Code (CSharp):
    1.     private void OnTriggerEnter(Collider other)
    2.     {
    3.  
    4.         Destroy(other.gameObject);
    5.         counter++;
    6.         Debug.Log(counter);
    7.         txtCounter.text = counter.ToString() + "/7";
    8.  
    9.         if (counter == CollectableItemCount)
    10.         {
    11.             txtGameOver.transform.gameObject.SetActive(true);
    12.             Debug.Log("game over");
    13.         }
    14.  
    15.     }
    worked fine after build. 2019.4.1f1 personal.
     
  20. oburacodacara

    oburacodacara

    Joined:
    May 17, 2020
    Posts:
    1


    What do you mean by restarting unity?
    Closing and opening?
    or there is another way to restart unity?
     
  21. Songshine

    Songshine

    Joined:
    Feb 8, 2016
    Posts:
    4
    In my case, in the same project, Collision (OnTriggerEnter) worked fine on v.5.6.2 (in the Editor and in the Build), but on v.2020.3.4 it worked ONLY in the Editor, but NOT in the Build.
    I checked the tags, checked it on another PC, and finally, with the help of Debag Log (Development Build), I found a suspicious part of the code, which seemed not to be called in the Build (but was called in the Editor).
    It turned out that this part of the code was called not from the main void Start, where the scene was loaded, but from the void Start of another script (where the Collision was described). For some reason, this did not prevent the scene from loading correctly in the Build in 5.6.2, but it did not work in the Build 2020.3.4. I just didn't pay attention to it earlier. I transferred everything needed to load the scene in one Start, and, in my case, everything began to work OK)