error : An instance of type "UnityEngine.MonoBehaviour

i get this error:
An instance of type “UnityEngine.MonoBehaviour” is required to access non static member “StartCoroutine”

i ma trying to use ZoomIn function that is in SceneControl script from another script

it works when i use from another script like this:

 StartCoroutine(SceneControl.ZoomIn());

but it doesnt work work when i have something like this:

 SceneControl.IsolateObject();

and in IsolateObject is the ZoomIn function
i tried with and without StartCoroutine, it just dont work, and it spits out this error.

SceneControl is just a script that holds static functions…

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):

static void RunCoroutine(GameObject param)
{
    MonoBehaviour someMonoBehaviour = param.GetComponent<MonoBehaviour>();

    someMonoBehaviour.StartCoroutine(DoStuff());
}

This will run DoStuff on someMonoBehaviour for as long as it isn’t destroyed.

no i am using javascript…is there a equivalent to this code? thanks!

My JS is a bit rusty, but something like this should work

static function RunCoroutine(param : GameObject)
{
    someMonoBehaviour = param.GetComponent(MonoBehaviour);

    someMonoBehaviour.StartCoroutine(DoStuff());
}

i am not sure how to implement your idea…

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).