• 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 joerosion · Sep 07, 2016 at 06:42 PM · triggercoroutineontriggerentertriggerscoroutines

Updating a Value in OnTriggerEnter and Returning That Value Immediately...

Hi everyone,

I've been getting familiar with Unity and scripting in C# recently, and I've come across a snag that I can't find/figure out a solution despite my searches here.

I'm trying to find a way to enable an object with a trigger and return a bool value that tells me if anything is within the trigger as the object is enabled. Trying to figure this out, I came to the conclusion that anything within an OnTriggerEnter function won't update until the next frame (I'm not entirely sure if this is correct).

So, to test this theory, I've set up a test case with two scripts involved. I'm attempting to use a Coroutine to delay the setting of the bool value to allow for the OnTriggerEnter function to execute. The first is a script that waits for my input. It's placed on a blank gameObject in the scene. When the input key is pressed ("f"), it sets the disabled gameobject to active and calls a function that begins a coroutine.

The first class:

 public class GlobalTriggerTest : MonoBehaviour {
     public GameObject foo;
     void Update () 
     {
         CheckForInput();
     }
     void CheckForInput()
     {
         if (Input.GetKeyDown(KeyCode.F))
         {
             foo.SetActive(true);
             Debug.Log(foo.GetComponent<TestingTriggers>().BeginPublicCoroutine());
         }
     }
 }

The second class, attached to a Cube gameObject in the scene. This cube is disabled right top of another, active cube. So when I active the object to which this script is attached, it is definitely colliding with something. Both cubes have kinematic rigidbodies:

 public class TestingTriggers : MonoBehaviour {
     bool triggerSwitch;
     public bool deliverableTriggerSwitch;
     public bool BeginPublicCoroutine()
     {
         StartCoroutine(DeliverBoolResults());
         return deliverableTriggerSwitch;
     }
     public bool returnTriggerSwitch()
     {
         return triggerSwitch;
     }
     IEnumerator DeliverBoolResults()
     {
         yield return new WaitForSeconds(5.0f);
         Debug.Log("I'm waiting for 5 seconds");
         deliverableTriggerSwitch = returnTriggerSwitch();
     }
     void OnTriggerEnter(Collider col)
     {
         triggerSwitch = true;
     }
     void OnTriggerExit(Collider col)
     {
         triggerSwitch = false;
     }
 }


There's a WaitForEndOfFrame that I could try using, but I wanted the function to be dramatic enough for me to see the results. Currently, however, when I press f, the prints a value of false to the console, even if the object is clearly within the bounds of anothe, waits 5 seconds, and then prints the Log message in the Coroutine to the console.

I'm not sure that this is the proper solution to my overarching issue, so if my approach is all wrong, please feel free to steer me in a different direction. Ultimately, I'm trying to find a way to be able to return a bool with its value tied to whether a trigger has detected any kind of entry as it's enabled.

Thanks very much everybody!

Comment
Add comment · Show 3
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 SterlingSoftworks · Sep 07, 2016 at 11:03 PM 0
Share

Upon turning on, a collider needs at least one FixedUpdate to occur in order to register a collision, so there's that.

Second, you could attach a script to the collider you're trying to check this stuff with called "ActiveCheck" (or whatever)

It could have a List activeObjects;

That gets added to in the OnTriggerEnter function (activeObjects.Add(collider.gameObject))

Then get's removed from in OnTriggerExit (activeObjects.Remove(collider.gameObject))

Then if you want to check all active objects, just loop through the activeObjects list and check if they're enabled :)

(This is what my current project is using for a grid based attack system)

avatar image joerosion · Sep 08, 2016 at 01:27 AM 0
Share

Thanks for the reply, @SterlingSoftworks

I really like the list idea and I'll definitely be employing that. However, as you mentioned, I need to wait at least one FixedUpdate in order for the collision to register--I'm trying to make that happen with coroutines, but I can't seem to figure out how. Even if I put some delay into the Coroutine, as I have above, the function responsible for returning a value immediately returns a value--it doesn't wait for the coroutine to resolve.

avatar image SterlingSoftworks joerosion · Sep 08, 2016 at 02:24 AM 0
Share

@joerosion Have you tried using OnTriggerStay()? It's like Update/FixedUpdate/LateUpdate but continually checks the stuff that's inside the collider.. I'm sure that's more what you need rather than using coroutines.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Coroutine is jerky after exiting trigger zone 1 Answer

How do Coroutines behave with Triggers? 1 Answer

decreasing volume when other sound plays 0 Answers

OnTriggerEnter doesnt see enemy 1 Answer

How do I ignore trigger objects for collision? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges