Optimisation method stop ?

Hi, I have a small question, if I use a method like InvokeRepeating in my START.

and invoke a VOID under a condition.

Once my condition changed, my VOID is empty.

What I would like to know is how Unity interprets this.

As my method instantiated in my START, even if it no longer invokes anything, it remains there.

Does this have any impact on game performance ?

If so, how to optimize this?

Switch to a coroutine.

I already use a coroutine to change my bool. I didn’t understand what you wanted to tell me.

I instantiate a coroutine that launches InvokeRepeating ?
Does this have a significant impact on performance ?

I’m saying switch it to a coroutine if you want to stop it.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html

You mean aside from the code that is running in the method? No.

1 Like

There is no such thing as “a void” in C#. There are methods.

InvokeRepeating continues to invoke the method perpetuity until StopInvoke is called or the GameObject is destroyed or deactivated. It is not possible to change the contents of a method at runtime, so I’m not sure what you mean by:

If you’re saying there’s an if (condition) in the method, then that condition will run and be evaluated whenever the method runs, naturally.

2 Likes

Ok I understand better, thank you, I haven’t slept much lately, and my cognitive faculties are weak ^^

PraetorBleu
Yes null is not appropriate, but I understood what you said.

Better then that I use:
CancelInvoke();
This will optimize rather than just a bool, that was the point of my question.

I’m learning little by little like everyone else ^^ The world of unity is vast, it’s going to take me a little time before assimilating the essentials ^^

Thank you very much I learned a new trick, even if it was logical, and I could have done the deduction on my own, tonight I have a little trouble ^^

Ryia

I just read a forum that also talks about passing it as a coroutine.

At the moment my use of coroutine is limited to WaitForSeconds, so I may not understand what you are trying to tell me, due to my lack of knowledge.

I spend my days reading APIs, but I can’t remember yet and I don’t understand everything.

By that I meant, if your method of using a couroutine was more beneficial in terms of performance, than using another way of doing it.

So I have another question. (nothing to do with previous messages)

If I run my coroutine in START, and set it to WaitForSeconds() then I just change the value of a variable.

I need to stop my coroutine? Since it will only be read once at startup?

You don’t have to stop it. You can just leave it alone. Like @PraetorBlue said though it will keep executing.

On every reboot?

InvokeRepeating continues to run until cancel…

But a coroutine is only read once?

My sole reason for recommending a coroutine is that you can stop it. You seemed to want to have a way to stop the InvokeRepeating in your original post.

Oh okay thank you.
My question was very badly posed, because I don’t understand something, now it’s clearer.
So for the coroutine I still don’t understand, but I guess to tell you that it uses resources, up to its stop.

So, I found for the invokes:
CancelInvoke() and CancelInvoke(methodName: string)

But I heard you, coroutines are simpler :slight_smile: