Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jpatt1990 · Mar 17, 2013 at 04:36 PM · errornullsave game

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;
 }
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 17, 2013 at 05:01 PM 1
Share

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....

avatar image whydoidoit · Mar 17, 2013 at 10:49 PM 0
Share

I don't see your error message or on what line it occurs :S I have formatted your code for you...

avatar image Jpatt1990 · Mar 18, 2013 at 12:27 AM 0
Share

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.

avatar image Jpatt1990 · Mar 18, 2013 at 12:31 AM 0
Share

I will try That 010101 thing and see what I can do, wasnt aware you could do that

avatar image Chronos-L · Mar 18, 2013 at 12:57 AM 1
Share

@jpatt1990, You can't say you believe that the error was on the line 123. Post/screenshot the whole error message here.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Bunny83 · Mar 18, 2013 at 01:13 AM

Well, if it's the line 123 it's pretty obvious:

ReadLine:

     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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jpatt1990 · Mar 18, 2013 at 02:14 AM 0
Share

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

avatar image Jpatt1990 · Mar 18, 2013 at 02:16 AM 0
Share

A jpeg not Josef lol my god I hate iPhone autocorrect

avatar image Chronos-L · Mar 18, 2013 at 02:34 AM 0
Share
    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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Null Reference exception when calling using "script.gameObject.GetComponent 1 Answer

object referance problem with bullet adding 1 Answer

script trying to access a null gameobject's collider... can't fix 2 Answers

NullReferenceException: object not in inspector? 0 Answers

SplatMap error 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges