hey so im a total noob when it comes to programming but for whatever reason they’re making me do it for school, im a modeler why am i doing this???, but oh well. anyways the problem im having is in regards to save games, the script is just adding scenes to the first scene as you complete the missions. i keep getting this error msg saying Argument cannot be Null. i attached a copy of the error msg if that helps basically what i want to do is set default values for the new game and create checkpoints once you get to certain points in the level.
i just copy and pasted the game manager script im working with, any help would be very much appreciated. i asked my teacher about this problem and he replied with
There may not be anything in the array. Make sure there are values in the array for all the missions. but not entirely sure what he means by this.
import System.IO; // used to write to files
static public var PlayerPrefab : GameObject;
public var PlayerPrefab2 : GameObject;
// where the Save file should be saved
static public var FilePath : String = "./Saves/SavedGame.txt";
//player info
private var PlayerHealth : int;
private var AmmoPrime : int;
private var AmmoAlt : int;
// what level is the Player on
static private var currentLevel : int;
// is the Player alive or dead
static public var levelState : String;
// what the status of the mission is, 1 is done and 0 is not
static private var Missions : int[] = [0, 0, 0];
// all spawnpoints in the level
static public var CurrentSpawnPointIndex : int = 1;
function Awake()
{
// stop the WorldManager from being destroyed
DontDestroyOnLoad(gameObject);
// set the player to the WorldManager so it can be read from anywhere
PlayerPrefab = PlayerPrefab2;
}
static function SpawnPlayer(spawnIndex:int)
{
// find the place to spawn the Player
var SpawnPlace:GameObject = GameObject.FindWithTag("spawnPoint" + CurrentSpawnPointIndex);
// was it found?
if(SpawnPlace)
{
// yes it was found, check if the Player prefab exists
if(PlayerPrefab)
{
// if it exists, create it
Instantiate(PlayerPrefab,SpawnPlace.transform.position,Quaternion.identity);
}
else
Debug.LogError("Player Prefab is not set or spawnpoint is not found");
}
else
Debug.Log("Spawnpoint wasn't found");
}
///////////////////////////////////////////////////////////////
// SAVE / LOAD SYSTEM
///////////////////////////////////////////////////////////////
static public function SaveGame( )
{
// open the file for writing
var file = new StreamWriter( Application.dataPath + "/Saves/SavedGame.txt" );
file.WriteLine( currentLevel ); // write the level the Player is on to the file
file.WriteLine( CurrentSpawnPointIndex ); // write the Player SpawnPoint to the file
// check if if the player exists so the information can be stored in the file
if (GameObject.FindWithTag("Player"))
{
// grab the Stat file to grab the information
//var StuffToWrite = GameObject.FindWithTag("Player").GetComponent("PlayerStats");
var PlayerStats:PlayerStats = GameObject.FindWithTag("Player").GetComponent("PlayerStats");
file.WriteLine( PlayerStats.GetHealth()); // write the health of the Player to the file
file.WriteLine( PlayerStats.GetAmmo(0)); // write the Primary Ammo of the Player to the file
file.WriteLine( PlayerStats.GetAmmo(1)); // write the Secondary Ammo of the Player to the file
}
// write the Mission status to the file
file.WriteLine( Missions[0] ); //level 1
file.WriteLine( Missions[1] ); //level 2
file.WriteLine( Missions[2] ); //level 3
// make sure to write the information to the file
file.Flush();
// close the file
file.Close();
}
public function SetStats( PlayerHealth : int ,AmmoPrime : int, AmmoAlt : int)
{
var PlayerStats : PlayerStats = GameObject.FindWithTag("Player").GetComponent("PlayerStats");
PlayerStats.AddHealth(PlayerHealth, 0);
PlayerStats.AddAmmo(0,AmmoPrime, 0);
PlayerStats.AddAmmo(1,AmmoAlt, 0);
}
public function Initialize()
{
// set the state to Playing
SetLevelState("Playing"); // used when working with coded buttons
// load saved gameinfo if exists
if( !Directory.Exists( "Assets/Saves/" ) )
{
// if the directory doesnt exist, create it
Directory.CreateDirectory( "Assets/Saves/" );
}
// check to see if the file exists before tryign to read information
if (File.Exists(Application.dataPath + "/Saves/SavedGame.txt"))
{
// file exists, open the file for reading
var file = new StreamReader( Application.dataPath + "/Saves/SavedGame.txt" );
// read the information stored in the file and store them in variables
currentLevel = parseInt(file.ReadLine());
CurrentSpawnPointIndex = parseInt(file.ReadLine());
PlayerHealth = parseInt(file.ReadLine());
AmmoPrime = parseInt(file.ReadLine());
AmmoAlt = parseInt(file.ReadLine());
Missions[0] = parseInt(file.ReadLine());
Missions[1] = parseInt(file.ReadLine());
Missions[2] = parseInt(file.ReadLine());
// close the file
file.Close();
}
// file doesnt exists, so create it with default values (New Game)
else
{
// set default values for the New Game being created
PlayerStats.SetInt("health",100);
PlayerStats.SetInt("ammoPrimary",20);
PlayerStats.SetInt("ammoSecondary",20);
// open the file for Writing
GetComponent(Application.dataPath + "/Saves/SavedGame.txt");
// write the information to the file
}
// load levels with the default or loaded values
LoadingLevels();
while(1)
{
var SpawnPlace : GameObject = GameObject.FindWithTag("spawnPoint" + CurrentSpawnPointIndex);
if(Application.GetStreamProgressForLevel(currentLevel) == 1 && SpawnPlace != null)
{
SpawnPlayer(CurrentSpawnPointIndex);
SetStats(PlayerHealth, AmmoPrime, AmmoAlt);
break;
}
else
{
yield WaitForSeconds(1);
}
}
}
//function used to load levels when game starts
function LoadingLevels()
{
// check mission 1 status, if its not done, load level 1
if(!Missions[0])
{
Application.LoadLevel(currentLevel);
}
// check mission 2 status, if its not done, load level 1 and 2
else if(!Missions[1])
{
Application.LoadLevel(currentLevel);
Application.LoadLevelAdditiveAsync(currentLevel+1);
Debug.Log("Loading Level 1");
}
// check mission 3 status, if its not done, load level 1, 2 and 3
else if(!Missions[2])
{
Application.LoadLevel(currentLevel);
Application.LoadLevelAdditive(currentLevel - 1);
//Application.LoadLevelAdditive(currentLevel - 2);
Debug.Log("Loading Level 2");
}
}
//frontend
function OnGUI()
{
if (Application.loadedLevel == 0 ) // start a New Game
{
// Create the Start button through code.
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "New Game/ Continue"))
{
// start the game from where the player last did well
Initialize();
}
}
else if (levelState == "Dead") // is the Player dead
{
// Create the Load Last Checkpoint button through code
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2, 180, 20), "Load last checkpoint"))
{
// start the game from where the player last did well
Initialize();
}
// Create the Go to Main Menu button through code
if(GUI.Button(Rect(Screen.width - Screen.width/2, Screen.height - Screen.height/2 + 30, 180, 20), "Go To Main Menu"))
{
// load the Start Screen
Application.LoadLevel(0);
}
}
}
static public function MissionStatusCheck (missionIndex : int)
{
// what is the status of the mission
return Missions[missionIndex];
}
// function is used to change the mission status, was it passed?
static public function SetMissionStatus( missionIndex : int, status : int)
{
// status in the array is updated
Missions[missionIndex] = status;
}
//changing level states
static function SetLevelState(newState : String)
{
levelState = newState;
}
static public function SetCheckPoint( newCheckPoint : int )
{
CurrentSpawnPointIndex = newCheckPoint;
}
static public function SetCurrentLevel (newLevel : int)
{
currentLevel = newLevel;
}