- Home /
OnTriggerStay Problem with rate?
Hi, I have a simple script here :
function OnTriggerStay (col : Collider) { RateCount += Time.deltaTime;
if (RateCount >= Rate){ col.SendMessageUpwards("DOthis", damage, SendMessageOptions.DontRequireReceiver);
RateCount = 0.0f;
} }
Basically what I want this script to do is send a message with a rate count to everything in the trigger, bu it only sends a message individually to each object in the trigger with a rate. The wierd thing is, if I remove the "rate" part, the script works fine sending messages to everything in the trigger. Been searching the forums for hours, still no luck. Please help in JS, Thanks!!!
Answer by TheDemiurge · May 23, 2011 at 08:54 AM
The OnTriggerStay
function gets called once, per frame, for each collider inside the trigger (maybe only once per GameObject if it has more than one collider, I'm not sure). SendMessageUpwards will only call DOthis for that object and its parents, not any children or 'sibling' objects, and certainly not any random objects.
If you need to send a message to multiple objects, you can either keep a separate list of objects, or obtain the list anew when RateCount>Rate, using something like GameObject.FindGameObjectsWithTag(..)
or similar method, to gather all the proper objects that should receive the message.
If you remove the rate part, what you get is simply each object in the trigger receiving the message individually. With the rate part, you're resetting RateCount when it >= Rate, so only the very first object will ever get that message, per 'iteration'.
Yes,that was exactly what I was looking for. Besides FindObjectsOfType and FindGameObjectsWithTag are there any other functions I could use to return a list of active objects?
there's Object.FindObjectsOfType and, perhaps more useful to you, Physics.OverlapSphere, which specifically finds colliders within a given position+radius, and can also follow the layer-based collision. In fact based on the above, you could probably drop the OnTriggerStay part and run a coroutine that yields until RateCount>=Rate, then uses Physics.OverlapSphere to communicate with nearby objects.
Lastly, you could implement your own classes to keep track of all objects of a certain type, if it suits your design.
Your answer
Follow this Question
Related Questions
Rate of health loss in trigger box. 1 Answer
OnTriggerStay simple question 1 Answer
My spawned obstacle stop working properly after a certain time 1 Answer
Combining Triggers 1 Answer
Inverse of OnTriggerStay? 1 Answer