static functions and yield

i trying to clean up the code i have now, so it doesn’t turn into one big mess, and currently i want to separate all functions that use yield (like fading,smoothing and those kind of things) into separate script, to make it static, because they are going to be used all over again from different scripts.

so, i made this script, named it YieldFunctions

function Update () {

}

static function Fade(col : Collider){

 var t = 0.0;
 while (t<1.0){
 t+=Time.deltaTime *(1.0/0.5);
 col.renderer.material.color.a=Mathf.Lerp(1.0,0.1,t);
 yield;
  }
}

i also made an empty game object, since i read that you need update to get yield work from static function. but now i dont know how to use it.

here is the example i tried (this piece of code is
located in other GO in update function):

 var hit : RaycastHit;
 if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit,100)){
 YieldFunctions.Fade(hit.collider);
}

but sadly this doesnt work…how to use get this done…?

The coroutine needs to run on a specific monobehaviour. Since static functions are not attached to one automatically by JS, you will need to do this manually by calling myBehaviour.StartCoroutine(coroutine). In your example:

if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),hit,100))
    StartCoroutine(YieldFunctions.Fade(hit.collider));

This will work as long as this raycast code is part of a monobehaviour (and not in a static function). If it isn’t, you will need a link to an existing coroutine and call StartCoroutine on that.

thanks, it works beautifully!

and it doesn’t need to be attached to the GO to work