- Home /
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!
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)
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.
@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.
Your answer
Follow this Question
Related Questions
Coroutine is jerky after exiting trigger zone 1 Answer
Can I make A trigger, Trigger the OnTriggerEnter Function 1 Answer
OnTriggerEnter is not working! I have tried absolutely everything I can think of. 1 Answer
Detect trigger touching another trigger? 1 Answer
decreasing volume when other sound plays 0 Answers