Coroutines need to run on a specific monobehaviour. Since static functions are not attached to one automatically, you will need to do this manually by calling someMonoBehaviourInYourScene.StartCoroutine(coroutine).
For example (since you mention StartCoroutine, I’m assuming you use C# here):
here is the part of code that illustrates what i have so far:
SceneControl script:
static var cam : MainCamera;
//NEEDED FOR CALCULATING DISTANCES
static var currentCameraDistance : float;
static var previousCameraDistance : float;
function Start(){
cam=GameObject.Find("MainCamera").GetComponent("MainCamera");
}
static function ZoomIn(distance : float,endDistance : float){
var t = 0.0;
while (t<1.0){
t+=Time.deltaTime *(1.0/0.2);
cam.distance=Mathf.Lerp(distance,endDistance,t);
yield;
}
}
static function FunctionWithZoom()
{
ZoomIn(start,end);
}
any other script where i want to use ZoomIn function from SceneControl script:
//this works but it doesnt give me flexibility:
StartCoroutine(SceneControl.ZoomIn(start,end));
//is i start the function from SceneControl that //uses ZoomIn function it doesnt work and it gives me //the error in the title...
SceneControl.FunctionWithZoom();
also, one more question…is this good way in keeping function that repeat through the game in one script, like this with static function…it just stroke me that i could use GetComponent to get the gameobject that holds this script, that way function wouldnt need to be static…is that right?
I’m not sure why your StartCoroutine line that works is not flexible, but you are right that using GetComponent and a non-static function will solve your problem. Alternatively, you could make your FunctionWithZoom() look for the SceneControl object in the scene. That way FunctionWithZoom can still be static (which, i assume, you want for convenient access).