How Do I pass an unknown class to a function?

**EDIT: SOLVED: Using delegates is way better than attempting to do what I was doing. See this video: C# Beginners Tutorial - 31 - Delegates - YouTube **

Howdy!

I’m attempting to create a stopwatch class that will allow me to yield for real world seconds, as opposed to Time.timeScale seconds–this is a problem because I’ll yield Waitforseconds in the game for 3 seconds, and then almost immediately for whatever reason the player might want to use a bullet-time function to slow down time. But the thing that was going to happen in three seconds will now no longer actually happen in 3 seconds–it will take hours in real time. So…long story short, I need to be able to pass some information to a yielding function that will then call a function from a string on the calling class. Most of that I’ve figured out–but there’s a slight problem calling the function that I’m not understanding. Here are the two classes I’m attempting to use. The error is: error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement

public class Howdy : MonoBehaviour {
	
	public SomewhereElse _somewhereElse;
	
	void Start() {
		_somewhereElse.CallSayHi<this.GetType()>("SayHi");
	}
	
	public void SayHi() {
		print("Hi!");	
	}
}

public class SomewhereElse : MonoBehaviour {
	
	
	public void CallSayHi<T> (string functionName) {
		MethodInfo mInfo = typeof(T).GetMethod(functionName);
		mInfo.Invoke(T, null);
	}
	
}

Thanks for your time…

Simon

Do you need that? If the goal is just to wait real-world seconds, all you have to do is yield on that custom coroutine the same way you would do WaitForSeconds. If you need to do other things when the coroutine is finished for some other unspecified reasons, then I’d recommend looking into delegates.

Generics are made for that. It allows you to use an undetermine type in a methode or a class.
This is how GameObject.GetComponent method works actually.

Try this:

void CallSomethingElse <T>(string functionName) 
{
   // then do whatever you want to do using T instead of callingclass
}

then you can call it this way:

myCallSomethingElseInstance.CallSomethingElse<my_undetermined_class>( my_function_name );

You can read more about generics here : http://msdn.microsoft.com/library/512aeb7t(v=vs.80).aspx

Hope it helps.