yield doesnt work in static functions?

Sorry if this is a noob question, but why doesnt yield WaitForSeconds (1) work within a static function.

I was making a “Hide/ShowForm” function for my GUI and I wanted to expose the function so I can manage all GUI forms from a Global script. Is there a way to call functions for a script in another object without using static?

Am I going about this all wrong? Any suggestions would be appreciated.

Cheers
Shaun

Not sure if I understand correctly. Your global script could do something like this in a static function:

guis = Object.FindObjectsOfType(GUIForm);
for(g in guis)
{
    g.Hide();
}

I’m sorry if this has been discussed somewhere else, I didn’t find that topic then, so I’m posting in this thread, which has a pretty long beard already.

I have exactly this Problem, using yield in a static function makes the whole script stop when the function is called.

Is there anything to do wrong?
Will this be made possible in the future otherwise?

Thanks in advance

I don’t think that it’s possible. I’ve tried it before, and it doesn’t work. Seems like it would be a useful feature though…

Yield can not work in static functions.
They are game object bound and a static function doesn’t have one technically.

I got the same problem.
“static function CMD03” just not working.

function OnGUI ()
{
	if(GUI.Button(Rect(0,0,50,20),"CMD01"))
		CMD02 ();
}

static function CMD02 ()
{
	CMD03();
	print("CMD02");
}

static function CMD03 ()
{
	yield WaitForSeconds (1);
	print("CMD03");
}

Yield can not work in static functions.
They are object bound and a static function doesn’t have any object.

It’s to be regretted that i have to reorganize some codes :x

:smile: Thank you for your attention again!

You can yield in static functions, you just need to make sure it has some MonoBehaviour to execute on. You will need to explicitly use StartCoroutine to make it run. This example runs the static Coroutine Bla on the MonoBehaviour that runs the Start function:

function Start()
{
	StartCoroutine(Bla());
}

static function Bla()
{
	Debug.Log(Time.time);
	yield WaitForSeconds(2);
	Debug.Log(Time.time);
}

That though does not make it a “static function” anymore basically if you execute it on a class instance instead of the class …

Also they can’t use local object data, restricting their use significantly, especially on coroutines.
None the less if you do this on a main manager class you can kick off some background managers (that are instance independent anyway) to handle stuff for example

I had the same issue. I’d like to give my solution then people won’t be frustrated by reading this thread.
My purpose was to pause the inputs during a defined time from any level.
In the “static” script I’ve written:

static var PauseInputsTime: float;

static function PauseInputs( seconds: float )
{
	PauseInputsTime=Time.time+seconds;
	input_active=false;
}

function Update(){
	if (!input_active  PauseInputsTime<Time.time){
		input_active=true;
	}
}

and in the script linked to the object requesting a pause, I’ve written:

Global.PauseInputs(1.0);

Global.js being the name of the “static” script.
Global.js had to be attached to the object of the level ( it’s required to be able use Update).

I hope that it will help.

Probably better than having a timer, but you still need an instance of the script somewhere:

static var thisScript : (thisScript's type)
function Awake(){
    thisScript = this;
}
private function InternalRemoveMissile(oldmis : Transform, waitTime : float){
	//thisScript.Invoke("RemoveMissile", oldmis, waitTime);
	//DebugConsole.LogMessage("Removing missile in "+waitTime);
	yield WaitForSeconds(waitTime); //apparently yield doesn't work in statics
	for (var i : int = 0; i < missileTransforms.length; i++){
		if (missileTransforms[i] == oldmis){
			missileTransforms.RemoveAt(i);
			//DebugConsole.LogMessage("Found missile. removing from list");
			return;
		}
	}
	DebugConsole.LogMessage("couldn't find the missile in the list. probably already removed it");
}

public static function RemoveMissile(oldmis : Transform, waitTime : float){
	thisScript.InternalRemoveMissile(oldmis, waitTime);
}

Wow, so this means that all WWW functions cannot be used in static functions?

How do you get this to work for a yield WWW?

I disagree, from a preliminary look it still has value.

For example I could have a static class full of static helper methods - all getting called from entirely different monobehaviours. No need to create an instance or to worry about managing that instance.

Of course it does, a static function does not “loose” it’s “staticness” just because one of the parameters you pass in is an object, or anything else. If the function is marked as static, it is a static function, simple as that. The keyword is not context sensitive.

I know what you’re trying to say, but you’re using the wrong terminology. Static functions do not have an implicit “this” parameter passed into them and they do not share an instance of any object among them (other then the typeof(Class) object, which is read-only anyway), but they can of course access data om object that are in local parameters and variables.