Search Unity

GameObject not colliding properly with a collider!

Discussion in 'Scripting' started by Bratveja, Apr 11, 2017.

  1. Bratveja

    Bratveja

    Joined:
    Mar 11, 2015
    Posts:
    29
    Hi! So I've got a gameObject that's colliding with a box collider! When this happens there is a canvas that's activating a text! Everything is okay till the moment when the two objects have to collide with each other... When the gameObject collide with the box collider the text I want to appear is doing it but only for a moment... Here is what I mean Gif > http://makeagif.com/i/uEU_M5

    and this is my code for the gameObject

    Code (CSharp):
    1.        public Text text;
    2.         public GameObject panel;
    3.    
    4.         void Start()
    5.         {
    6.             text = GameObject.Find("sewingCanvas/GameObject/Panel/Text").GetComponent<Text>();
    7.             panel = GameObject.Find("sewingCanvas/GameObject/Panel");
    8.             panel.SetActive(false);
    9.             text.gameObject.SetActive(false);
    10.         }
    11.    
    12.         void OnTriggerEnter(Collider co)
    13.         {
    14.                 if (co.tag == "archieoPlaced")
    15.             {
    16.                 panel.SetActive(true);
    17.                 text.gameObject.SetActive(true);
    18.                 text.text = "Правилно! ";
    19.             }
    20.             else if (co.tag == "shopPlaced" || co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "programmerPlaced")
    21.             {
    22.                 panel.SetActive(true);
    23.                 text.gameObject.SetActive(true);
    24.                 text.text = "Грешка! Опитайте с друг предмет ";
    25.             }
    26.    
    27.             else
    28.             {
    29.                 panel.SetActive(false);
    30.                 text.gameObject.SetActive(false);
    31.             }
    32.    
    33.         }
    34.    
    35.         IEnumerator OnTriggerStay(Collider co)
    36.         {
    37.    
    38.             yield return new WaitForSeconds(5);
    39.    
    40.             if (co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "archieoPlaced" || co.tag == "programmerPlaced")
    41.             {
    42.                     panel.SetActive(false);
    43.                     text.gameObject.SetActive(false);
    44.                     gameObject.GetComponent<Pickupable>().enabled = false;
    45.                
    46.             }
    47.         }
    48.    
    49.         void OnTriggerExit(Collider co)
    50.         {
    51.             if (co.tag == "shopPlaced" || co.tag == "schoolPlaced" || co.tag == "hospitalPlaced" || co.tag == "fitnessPlaced" || co.tag == "micPlaced" || co.tag == "shoesPlaced" || co.tag == "policePlaced" || co.tag == "firePlaced" || co.tag == "archPlaced" || co.tag == "archieoPlaced" || co.tag == "programmerPlaced")
    52.             {
    53.                 panel.SetActive(false);
    54.                 text.gameObject.SetActive(false);
    55.             }
    56.         }
    57.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Probably not directly related to your issue, but you should get rid of GameObject.Find and replace it. (Probably with a singleton in this case)

    You're being heavily over-reliant on tags. Again, I don't know for sure if that's your problem, but tags tend to make for long lines of unwieldy code and it's very easy for small mistakes to slip through the crack. I would recommend creating a MonoBehaviour (script) on these objects that currently have those tags (shopPlaced, schoolPlaced, etc), and use variables on that script to track the information you're currently keeping in tags. Then you can (I'm guessing, though I'd need more info on your project to say for sure) just have a single "bool isPlaced" that gets flipped, and replace those long if statements with
    Code (csharp):
    1. if (co.GetComponent<ThatScriptName>().isPlaced)
    (Don't be stingy with MonoBehaviours; they're lightweight. If you remove the empty Start() and Update() functions, a MonoBehaviour on an object costs you nothing in terms of performance.)

    One last thing that looks like a troublesome point is having OnCollisionStay be a coroutine. OnCollisionStay is called every frame while the things overlap, and if it's a coroutine, you're going to have hundreds of those functions running in parallel, and then they'll all start hitting and undoubtedly causing problems.

    I'm not sure what the issue is exactly, but getting these issues cleaned up A) will make your code easier to keep track of (and therefore diagnose the real problem), or B) has a decent chance of fixing the issue on its own.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Actually, watching the gif more closely I believe I know what the issue is (but still fix those other things). Your thing collides with the RIGHT thing, and activates the text; but then it collides with a DIFFERENT thing (floor), which sends your code into the final "else" of your "OnTriggerEnter" function.
     
  4. Bratveja

    Bratveja

    Joined:
    Mar 11, 2015
    Posts:
    29
    Hi! This worked thanks! And thanks for the advices too! For the OnCollisionStay I'm using it as a coroutine because of the fact that if the player place the right item at the right place I need the text to disappear. Same is if the item is on a wrong place!
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'd do that with a timer variable rather than a coroutine; like I said, you're going to have hundreds of those coroutines running in parallel and messing things up (worse, those coroutines aren't going to stop after they're no longer touching, or when it's touching something else).

    Try:
    Code (csharp):
    1. private float collisionEntered = -1f;
    2. void OnTriggerEnter(Collider c) {
    3. if (whatever) {
    4. collisionEntered = Time.time;
    5. }
    6. }
    7.  
    8. void OnTriggerStay(Collider c) {
    9. if (Time.time > collisionEntered + 5f) {
    10. //deactivate or whatever
    11. }
    12. }
    This will avoid those issues with have Stay as a coroutine.