I’m trying to call InvokeRepeating() in one of my classes:
public class TerrainPlayer : MonoBehaviour
{
void Update()
{
// refresh the terrains height so that terrain (and tree) collision happen correctly
// use "InvokeRepeating" to have it refresh over time, and not every frame
InvokeRepeating("RefreshCollider", 10.0f, 10.0f);
}
void RefreshCollider()
{
Terrain.activeTerrain.RefreshTerrainCollider();
}
}
public static class TerrainExtensions {
public static void RefreshTerrainCollider(this Terrain terrain)
{
Debug.Log("Refreshing the Terrain's height at: " + Time.time);
// Refresh the terrain's heights, ensuring the TerrainCollider remains accurate
float[,] heights = terrain.terrainData.GetHeights(0, 0, 0, 0);
terrain.terrainData.SetHeights(0, 0, heights);
}
}
I have set it to invoke the method (for testing purposes) 10 seconds from now and then again every 10 seconds.
It waits the 10 seconds to perform the first invocation, but then invokes the method every frame that follows.
Invoking that method is extremely costly which is why I only want it done every so many seconds, and not every update, hence InvokeRepeating()…
I’ve tried many other values like: 1, 1.0f, 2f, 2.2f etc. but the same thing happens.
Did I do something wrong? Is there a new or different method that will do the same thing? What will fix it so I can call the method once every x seconds?
Thanks!