Hi Unity Community,
Still new and have been going along well. I’m trying to serialise my player stats without using player prefs
Whenever I try to serialise, I get the error:
ArgumentNullException: Argument cannot be null. Parameter name: obj
The error points to an empty game object I created called MainMenu with the main menu script attached.
Being an empty game object should not be a problem because I tried running the SaveAll() function when the script was attached to the Main Camera and I still have the same error.
The main menu works fine with no errors, it only comes up with the error when it tries to serialize. I don’t get why because I’m not trying to serialise the main menu.
Below is my main menu script. This is just a learning gui, there are buttons that do nothing but again I don’t see how that would be an issue.
using UnityEngine;
using System.Collections;
public class MainMenuScript : MonoBehaviour
{
public GUISkin GameSkin;
private bool isFirstMenu = true;
private bool isSinglePlayerMenu = false;
private bool isMultiPlayerMenu = false;
private bool isOptionsMenu = false;
private bool isNewGameMenu = false;
private bool isCreateCharacterMenu = false;
//Character Creation Screen
public string playerName;
public string playerGender;
public string[] playerNationality;
public string[] playerClass;
public string selectedNationality;
public string selectedClass;
private bool editingNationality = false;
private bool editingClass = false;
//bool class
public bool isMageClass = false;
public bool isSoldierClass = false;
//Character Class Stats
private BaseClass class1 = new BaseSoldierClass();
private BaseClass class2 = new BaseMageClass();
void OnGUI()
{
GUI.skin = GameSkin;
GUI.Label(new Rect(30, 75, 300, 25), "Heroes : Man and Myth", "Menu Title");
FirstMenu();
SinglePlayerMenu();
MultiPlayerMenu();
OptionsMenu();
NewGameMenu();
CreateCharacterMenu();
if (isSinglePlayerMenu == true || isMultiPlayerMenu == true || isOptionsMenu == true)
{
if (GUI.Button(new Rect(10, Screen.height - 35, 150, 25), "Back"))
{
isSinglePlayerMenu = false;
isMultiPlayerMenu = false;
isOptionsMenu = false;
isFirstMenu = true;
}
}
if(isNewGameMenu == true || isCreateCharacterMenu == true)
{
if(GUI.Button(new Rect( 10, Screen.height - 35, 150, 25), "Back"))
{
isNewGameMenu = false;
isCreateCharacterMenu = false;
isSinglePlayerMenu = true;
}
}
}
void FirstMenu()
{
if (isFirstMenu)
{
if (GUI.Button(new Rect(10, Screen.height / 2 - 100, 150,25), "Singleplayer"))
{
isFirstMenu = false;
isSinglePlayerMenu = true;
}
if (GUI.Button(new Rect(10, Screen.height / 2 - 65, 150, 25), "Multiplayer"))
{
isFirstMenu = false;
isMultiPlayerMenu = true;
}
if (GUI.Button(new Rect(10, Screen.height / 2 - 30, 150, 25), "Options"))
{
isFirstMenu = false;
isOptionsMenu = true;
}
if (GUI.Button(new Rect(10, Screen.height / 2 + 5, 150, 25), "Quit"))
{
Application.Quit();
}
}
}
void SinglePlayerMenu()
{
if (isSinglePlayerMenu)
{
GUI.Label(new Rect(30, 150, 200, 50), "Character Selection", "Sub Menu Title");
GUI.Box(new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height), "");
if (GUI.Button(new Rect(20, Screen.height / 2 - 100, 200, 150), "Character 1"))
{
}
if (GUI.Button(new Rect(230, Screen.height / 2 - 100, 200, 150), "Character 2"))
{
}
if (GUI.Button(new Rect(440, Screen.height / 2 - 100, 200, 150), "Character 3"))
{
}
if (GUI.Button(new Rect(650, Screen.height / 2 - 100, 200, 150), "Character 4"))
{
}
if (GUI.Button(new Rect(20, Screen.height / 2 + 200, 150, 25), "Create New Character"))
{
isFirstMenu = false;
isSinglePlayerMenu = false;
isCreateCharacterMenu = true;
}
if (GUI.Button(new Rect(190, Screen.height / 2 + 200, 150, 25), "Launch Settings"))
{
isFirstMenu = false;
isSinglePlayerMenu = false;
isNewGameMenu = true;
}
}
}
void MultiPlayerMenu()
{
}
void CreateCharacterMenu()
{
if (isCreateCharacterMenu) {
GUI.Label (new Rect (30, 150, 200, 50), "Character Creation", "Sub Menu Title");
GUI.Box (new Rect (Screen.width / 2, 0, Screen.width / 2, Screen.height), "");
GUI.Label (new Rect (10, Screen.height / 2 - 200, 90, 25), "Player Name:");
playerName = GUI.TextField (new Rect (100, Screen.height / 2 - 200, 200, 25), playerName, 15);
GUI.Label (new Rect (10, Screen.height / 2 - 165, 90, 25), "Gender:");
if (GUI.Button (new Rect (100, Screen.height / 2 - 165, 60, 25), "Male")) {
playerGender = "Male";
}
if (GUI.Button (new Rect (170, Screen.height / 2 - 165, 60, 25), "Female")) {
playerGender = "Female";
}
//Nationality Selection
GUI.Label (new Rect (10, Screen.height / 2 - 130, 90, 25), "Nationality:");
if (GUI.Button (new Rect (100, Screen.height / 2 - 130, 90, 25), selectedNationality)) {
editingNationality = true;
}
if (editingNationality) {
for (int x = 0; x < playerNationality.Length; x++) {
if (GUI.Button (new Rect (100, (Screen.height / 2 - (26 * x)), 90, 25), playerNationality [x])) {
selectedNationality = playerNationality [x];
editingNationality = false;
}
}
}
//Class Selection
GUI.Label (new Rect (220, Screen.height / 2 - 130, 90, 25), "Class:");
if (GUI.Button (new Rect (280, Screen.height / 2 - 130, 90, 25), selectedClass)) {
editingClass = true;
}
if (editingClass) {
for (int x = 0; x < playerClass.Length; x++) {
if (GUI.Button (new Rect (280, (Screen.height / 2 - (26 * x)), 90, 25), playerClass [x])) {
selectedClass = playerClass [x];
editingClass = false;
}
}
}
// Player Stat Labels
GUI.Label (new Rect (10, Screen.height / 2 + 50, 140, 25), "Character Description:");
GUI.Label (new Rect (10, Screen.height / 2 + 80, 140, 25), "Character Stats:");
GUI.Label (new Rect (10, Screen.height / 2 + 110, 140, 25), "Strength");
GUI.Label (new Rect (10, Screen.height / 2 + 140, 140, 25), "Vitality");
GUI.Label (new Rect (10, Screen.height / 2 + 170, 140, 25), "Dexterity");
GUI.Label (new Rect (10, Screen.height / 2 + 200, 140, 25), "Endurance");
GUI.Label (new Rect (10, Screen.height / 2 + 230, 140, 25), "Inteligence");
GUI.Label (new Rect (10, Screen.height / 2 + 260, 140, 25), "Charisma");
//Shows Player Stats
if (selectedClass == "Mage") {
isMageClass = true;
GUI.Label (new Rect (170, Screen.height / 2 + 50, 200, 25), class1.PlayerClassDescription);
GUI.Label (new Rect (170, Screen.height / 2 + 110, 200, 25), class1.PlayerStrength.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 140, 200, 25), class1.PlayerVitality.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 170, 200, 25), class1.PlayerDexterity.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 200, 200, 25), class1.PlayerEndurance.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 230, 200, 25), class1.PlayerInteligence.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 260, 200, 25), class1.PlayerCharisma.ToString ());
}
if (selectedClass == "Soldier") {
isSoldierClass = true;
GUI.Label (new Rect (170, Screen.height / 2 + 50, 200, 25), class2.PlayerClassDescription);
GUI.Label (new Rect (170, Screen.height / 2 + 110, 200, 25), class2.PlayerStrength.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 140, 200, 25), class2.PlayerVitality.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 170, 200, 25), class2.PlayerDexterity.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 200, 200, 25), class2.PlayerEndurance.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 230, 200, 25), class2.PlayerInteligence.ToString ());
GUI.Label (new Rect (170, Screen.height / 2 + 260, 200, 25), class2.PlayerCharisma.ToString ());
}
if ((playerName != "") && (playerGender != "")) {
if (GUI.Button (new Rect (170, Screen.height - 35, 150, 25), "Create Player")) {
isNewGameMenu = false;
isCreateCharacterMenu = false;
isSinglePlayerMenu = true;
//GameObject.Find("Main Camera").GetComponent<CreateNewCharacter>().NewCharacter ();
CreateNewCharacter.Instance.NewCharacter ();
}
}
}
}
void NewGameMenu()
{
if (isNewGameMenu)
{
GUI.Label(new Rect(30, 150, 200, 50), "Level Select", "Sub Menu Title");
if (GUI.Button(new Rect( 20, Screen.height / 2 - 100, 200, 150), "Act 1"))
{
}
if (GUI.Button(new Rect(230, Screen.height / 2 - 100, 200, 150), "Act 2"))
{
}
if (GUI.Button(new Rect(440, Screen.height / 2 - 100, 200, 150), "Act 3"))
{
}
if (GUI.Button(new Rect(650, Screen.height / 2 - 100, 200, 150), "Act 4"))
{
}
if (GUI.Button(new Rect(20, Screen.height / 2 + 200, 150, 25), "Normal"))
{
}
if (GUI.Button(new Rect(190, Screen.height / 2 + 200, 150, 25), "Hard"))
{
}
if (GUI.Button(new Rect(360, Screen.height / 2 + 200, 150, 25), "Nightmare"))
{
}
if (GUI.Button(new Rect(530, Screen.height / 2 + 200, 150, 25), "Godly"))
{
}
if (GUI.Button(new Rect(20, Screen.height / 2 + 300, 150, 25), "Begin"))
{
Application.LoadLevel("Level01");
}
}
}
void OptionsMenu()
{
if (isOptionsMenu)
{
GUI.Label(new Rect(30, 150, 200, 50),"Options", "Sub Menu Title");
GUI.Box(new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height), "");
if (GUI.Button(new Rect(10, Screen.height / 2 - 30, 150, 25), "Audio Options"))
{
}
if (GUI.Button(new Rect(10, Screen.height / 2 + 5, 150, 25), "Graphics Options"))
{
}
}
}
}
I’m not sure what it is about the code that is throwing the exception.
The CreateNewCharacter script is passing all stats I want to the PlayerSlot1 script where I want the data to save.
This is the serialisation script:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveInformation {
public static PlayerSlot1 SaveData;
public static void SaveAll()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/SaveData.df");
bf.Serialize (file, SaveInformation.SaveData);
file.Close ();
Debug.Log ("Application.persistentDataPath");
}
}
I’ve been searching for the last two days trying to find out what the problem is. I’m hoping someone out there can help. Thanks in advance.