- Home /
Argument cannot be Null??,
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;
}
Are you sure you don't have more on your script?
What about isolating the part that is relevant so that people won't have to read a novel of code not laid out properly?
$$anonymous$$ore seriously, do look for the part where your error is, remove the rest and use the 010101 button to display as code. You are unlikely to get any help with such a loooooong thing like this. We are only human....
I don't see your error message or on what line it occurs :S I have formatted your code for you...
Hey sorry guys but just so you're aware I didn't write this code my professor did and gave it to the class to work with; but before we can finish the lab we have to fix his code apparently. I believe the error message was on the 123 line, and I've asked my $$anonymous$$cher about this and he basically said check the values in the mission array and that didn't really help.
I will try That 010101 thing and see what I can do, wasnt aware you could do that
@jpatt1990, You can't say you believe that the error was on the line 123. Post/screenshot the whole error message here.
Answer by manilamerc · Mar 18, 2013 at 04:03 PM
Your 8 lines in the txt. file should be like this
1 1 100 0 0 0 0 0
Answer by Bunny83 · Mar 18, 2013 at 01:13 AM
Well, if it's the line 123 it's pretty obvious:
Return Value
The next line from the input stream, or a null reference
if the end of the input stream is reached.
Since you get an argument null reference exception i simply gueyy the file you're reading does not contain the data the script is expecting.
May I ask how many lines your "SavedGame.txt" has?
Just like to add that this is just an assumption based on your statement that the error is in line 123 and that the line 123 in your script corresponds to the line 123 here in the script.
Interesting when I first posted this question I thought I uploaded a Josef of the error message, will re upload once back on my pc. I believe the txt file has 6 lines in it
currentLevel = parseInt(file.ReadLine());
CurrentSpawnPointIndex = parseInt(file.ReadLine());
PlayerHealth = parseInt(file.ReadLine());
AmmoPrime = parseInt(file.ReadLine());
AmmoAlt = parseInt(file.ReadLine());
$$anonymous$$issions[0] = parseInt(file.ReadLine());
$$anonymous$$issions[1] = parseInt(file.ReadLine());
$$anonymous$$issions[2] = parseInt(file.ReadLine());
The code wants to read 8 lines of data, but there is just 6 lines in the .txt. See the problem with that?