Is there anyway I could make a static recursive function call itself in sync with Unity’s Update cycles?
Edit: Heres a code example of roughly what I was trying todo. I’ve now found out it’s a bad idea. See comments from @Statement and @Bunny83 for reasons.
public static class Manager
{
private Boolean _enabled = false;
public Boolean Enabled
{
set{
if (value== true)
{
Start();
}
else
{
Stop();
}
_enabled = value;
}
}
public void Start(
{
enabled = true;
DoSomething();
}
public void Stop()
{
enabled = false;
}
private void DoSomething()
{
if(enabled)
{
//Do somthing on GameObjects.......
//Recurse - ** WANT TO SYNC THIS CALL WITH UNITY UPDATE CYCLES **
DoSomething();
}
}
}
Generally recursive calls are considered bad, and eat up stack memory like no tomorrow. Coupled with Coroutines it sounds like a bad idea. What is it that you are trying to solve really?
Not quite sure what you mean, but I’ll give my best shot at it.
void Example()
{
StartCoroutine(Recursive(this, 10));
}
static IEnumerator Recursive(MonoBehaviour s, int value)
{
if (value <= 0) yield break; // Will exit the coroutine
yield return null; // Wait for next frame
// Do any processing if you wish, then call recursively again using g.
Debug.Log(value, s);
yield return s.StartCoroutine(Recursive(s, value - 1));
}
Well, i totally forgot the "static" in the quesiton :) StartCoroutine is not a member of GameObject. It's a member of MonoBehaviour so you need to pass one to the function. ("this" will do :D)
That doesn't make much sense. Recursive functions need a termination-condition since each recursion will consume memory which is freed when the whole recursion is finished. Even when you use coroutines to "sync" with Update you will have the same problem.
If you have a well planed recursion (with exitcondition) just turn it into a coroutine and do a `yield;` (Unityscript) or `yield return null;` (C#) to wait for the next frame.
static function MyRecursion(script : MonoBehaviour, value : int)
{
Debug.Log("MyRecursion Start :" + value);
yield;
if (value > 0)
{
yield script.StartCoroutine(MyRecursion(value - 1));
}
Debug.Log("MyRecursion End :" + value);
}
function Start()
{
MyRecursion(this,3);
}
This will run one recursion each frame and the output should look like this:
MyRecursion Start :3 // frame 1
MyRecursion Start :2 // frame 2
MyRecursion Start :1 // frame 3
MyRecursion Start :0 // frame 4
MyRecursion End :0 // all returns will also happen in the frame 4
MyRecursion End :1 // ..
MyRecursion End :2 // ..
MyRecursion End :3 // ..
(This script has not been tested yet!)
Did you changed collision type to Continious? Are you sure the problem is the collision rate? I'm sure that is not the problem. Are you sure the collisions are beeing detected? At any speed?
Generally recursive calls are considered bad, and eat up stack memory like no tomorrow. Coupled with Coroutines it sounds like a bad idea. What is it that you are trying to solve really?
– Statement