- Home /
Storing different scripts into a list iterating issue
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:
Assets/5_scripts/1 game_global/SaveManager.js(7,17): BCE0005: Unknown identifier: 'i'.
I don't know javascript well, but shouldn't this line:
var i : $$anonymous$$onoBehaviour
be written like
var i
or does it not matter?
Answer by Slev · Nov 21, 2014 at 03:20 PM
You have a semicolon after your loop clause. That's the problem.
@Immanuel-Scholz I spent a good three $$anonymous$$utes staring at it before I noticed... semicolons will get you every time.
Answer by Immanuel-Scholz · Nov 21, 2014 at 03:24 PM
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();