- Home /
SendMessage every second?
Hi,
I'm using "SendMessage("decrease", rate, SendMessageOptions.DontRequireReceiver)" within an update function but I don't want it to occur every frame, instead only once a second. Also I only want it to be sent while the jump button is held down.
How can I do this?
Thanks, Stu
I would also like the var "rate" to be larger, the faster the rigidbody is going. Something like "rate * rigidbody.velocity" is this possible?
Answer by save · Sep 02, 2011 at 08:35 AM
You should invoke the call for timed functions, running a function every frame will still eat some CPU.
function Start () {
InvokeRepeating("sendDecreaseMessage", 0, 1);
}
function sendDecreaseMessage () {
SendMessage("decrease", rate, SendMessageOptions.DontRequireReceiver);
}
$$anonymous$$eep in $$anonymous$$d that Send$$anonymous$$essage is more expensive than a direct call to the GameObject(s).
Cache the target component and access it directly (if it's possible):
private var target : theComponent;
target = GetComponent(theComponent);
Then you can use
target.decrease();
Thanks for your help save. Just one more thing. I would also like the var "rate" to be larger, the faster the rigidbody is going. Something like "rate * rigidbody.velocity" is this possible?
Yeah sure it's very possible, just keep in $$anonymous$$d that you should clamp the rate value to a maximum.
What is it exactly you want to do? There might be better solutions for this.
Answer by Ludiares.du · Sep 02, 2011 at 08:31 AM
function Update()
{
Send();
}
function Send()
{
yield WaitForSeconds(1);
if(Input.GetButton("Jump"))
{
SendMessage("decrease", rate, SendMessageOptions.DontRequireReceiver);
}
}
That won't work. It will start a new instance of the coroutine every frame.
Your answer
Follow this Question
Related Questions
Pass Enum To Function Parameter 2 Answers
BroadcastMessage 1 Answer
How to Send Message to other GameObject 3 Answers
Flame Thrower - Particle Collision Help 1 Answer
How do i Specify a Receiver for a Send Message Function? 2 Answers