I am trying to call the same function on different scripts.
I place all scripts into an array and then try to call that function by iterating trough all scripts.
private var SavableScripts : List.<MonoBehaviour> = new List.<MonoBehaviour>();
public function SaveNewGame() : void
{
5. for ( var i : MonoBehaviour in SavableScripts );
{
i.OnSaveGame();
}
10. // 2do hide saving icon
}
This does not seem to work, it can’t find that member function OnSaveGame().
How can I accomplish this, do I have to look into generic functions using c# instead of this unityscript attempt of mine?
I do not wish to call BroadcastMessage and the like because I will not know when saving is completed that way.
Bunny83
2
You basically have 4 options:
- Use Invoke on that MonoBehaviour reference.
- Use a base class with a virtual method that gets overridden in your derived classes
- Use an interface (that has to be defined in C# as that’s not possible in UnityScript afaik) and let your classes implement that interface.
- Use reflection to sneak into the acual class, find the method you want to call and invoke it manually.