I am working on a safe system for my game,
I store all scripts that need data saved to a list so when the player calls a save I can iterate trough the scripts that have a OnSaveGame function.
private var SavableScripts : List.<MonoBehaviour> = new List.<MonoBehaviour>();
public function SaveNewGame() : void
{
for ( var i : MonoBehaviour in SavableScripts );
{
i.OnSaveGame();
}
// 2do hide saving icon
}
I have plenty of these kind of for loops but this one returns the compile error:
I don’t know why the error says “Unknown identifier: ‘i’”, but the problem might be that MonoBehaviour just does not have a function named “OnSaveGame”.
You might either send a message to the game object instead:
SendMessage(i.gameObject, "OnSaveGame");
But this will be delivered to every component on the same gameObject as well - not only to the registered script.
So maybe its better you define an interface that contains the OnSaveGame function and cast i to that interface.
public interface ISaveGame {
function OnSaveGame();
}
...
(i as ISaveGame).OnSaveGame();