• 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 /
  • Help Room /
avatar image
Question by Bratveja · Apr 10, 2017 at 11:21 PM · c#scripting problemcollidercolliderscollision detection

GameObject not colliding properly with a collider!

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

and this is my code for the gameObject

    public Text text;
     public GameObject panel;
 
     void Start()
     {
         text = GameObject.Find("sewingCanvas/GameObject/Panel/Text").GetComponent<Text>();
         panel = GameObject.Find("sewingCanvas/GameObject/Panel");
         panel.SetActive(false);
         text.gameObject.SetActive(false);
     }
 
     void OnTriggerEnter(Collider co)
     {
             if (co.tag == "archieoPlaced")
         {
             panel.SetActive(true);
             text.gameObject.SetActive(true);
             text.text = "Правилно! ";
         }
         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")
         {
             panel.SetActive(true);
             text.gameObject.SetActive(true);
             text.text = "Грешка! Опитайте с друг предмет ";
         }
 
         else
         {
             panel.SetActive(false);
             text.gameObject.SetActive(false);
         }
 
     }
 
     IEnumerator OnTriggerStay(Collider co)
     {
 
         yield return new WaitForSeconds(5);
 
         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")
         {
                 panel.SetActive(false);
                 text.gameObject.SetActive(false);
                 gameObject.GetComponent<Pickupable>().enabled = false;
             
         }
     }
 
     void OnTriggerExit(Collider co)
     {
         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")
         {
             panel.SetActive(false);
             text.gameObject.SetActive(false);
         }
     }

Comment

People who like this

0 Show 0
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

3 Replies

  • Sort: 
avatar image

Answer by Matthewj866 · Apr 12, 2017 at 04:43 PM

Your problem is most likely in OnTriggerStay() since you are disabling the object when the two objects are still touching. It could also be related to OnTriggerExit() if the object leaves the collision unexpectedly, but as stated it's most likely the former.

Comment

People who like this

0 Show 0 · 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

Answer by Cuttlas-U · Apr 11, 2017 at 09:21 AM

Hi; in lane 25 u have a "else" function that set every thing to false if it collide some thing else ; so i think this happense and make your objects dissactive;

Comment

People who like this

0 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 Bratveja · Apr 11, 2017 at 11:48 AM 0
Share

Nope It's not from that! I need this one because when i pick the object in the inventory I need the text to disappear! I've actually tried to remove it but it didn't worked

avatar image

Answer by Davirtuoso · Apr 12, 2017 at 10:30 PM

Note: This post was edited as I did some research and revised my solution

I'm not 100% certain as i'm fairly new to unity and C# myself, but I don't believe that using OnTriggerStay() as an IEnumerator (line 35) would work properly like that, as it it is a method that has already been pre-defined elsewhere. If it works as I imagine it would, OnTriggerStay() would not run this line:

 yield return new WaitForSeconds(5);

As the method isn't inherently an IEnumerator, meaning it would move on to running the rest of the code immediately. This means if any of the 'if' statements that follow are true, the panel would be immediately disabled. The method will keep on running regardless of delays, even using a co-routine with this yield line included.

To get around this, i suggest calling a seperate Coroutine from within OnTriggerStay() to handle the entirety of your code instead of keeping it all in the Coroutine. Try try replacing OnTriggerStay() with something such as this:

 void OnTriggerStay(Collider co)
 {
      StartCoroutine(WaitForFive());
 }

Then add a seperate method to perform the task for you:

 IEnumerator WaitForFive()
 {
     yield return new WaitForSeconds(5);

 // Rest of the code you want to execute after the delay
 }

I haven't tested the code above directly, but hopefully it gives you the right idea to solve your problem. Good luck!

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

352 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How do I check if a GameObject is within or colliding with an arc? 1 Answer

Ignore collision based on position 1 Answer

How To how ?? 2 Answers

Collision With Text 0 Answers

No overload for method. 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