Object reverence not set? (why?)

Updated on July 20, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
15 on July 1, 2017

the error:
NullReferenceException: Object reference not set to an instance of an object
LoadAndSaveAGame.LoadTheGame () (at Assets/Scripts/SaveStuff/LoadAndSaveAGame.cs:96)
GameStart.Awake () (at Assets/Scripts/GameStart.cs:11)


and because it can be a stupid mistake I cant find:

the Load and save a game code: (short)

    public void LoadTheGame() {
        if (SaveGameSystem.DoesSaveGameExist("saveFile") == true) {
            MySaveGame saveFile = SaveGameSystem.LoadGame ("saveFile") as MySaveGame;
            PlayerStats.MoneyFloat = saveFile.MoneyFloat;

and the Game start code:

 using UnityEngine;
public class GameStart : MonoBehaviour {
    public LoadAndSaveAGame loadAndSaveAGame;
    void Awake () {
        loadAndSaveAGame.LoadTheGame ();

and the mysavegame code:

using System;
public class MySaveGame : SaveGame
    public float MoneyFloat { get; set;}


Save code: (This one is working fine :>)

    public void SaveTheGame (){
    MySaveGame saveFile = new MySaveGame ();
        saveFile.MoneyFloat = PlayerStats.MoneyFloat;
        SaveGameSystem.SaveGame(saveFile, "saveFile");
        Debug.Log ("saved...");

my problem is, i cant find what i am doing wrong… i hope my mistake is not that stupid….


would be nice if you can help me ^^

  • Liked by
2 on July 3, 2017

 I still didnt  found my mistake… (the error first showed after i added new stuff)

on July 4, 2017

Hi ererbe,

You are creating a public Gameobject variable of type LoadAndSaveAGame in your Gamestart.cs script.

public LoadAndSaveAGame loadAndSaveAGame;

Did you drag a Gameobject(Prefab) that has the LoadTheGame() method in his script into the reference box in the Unity UI screen? (Somewhere in the Inspector at the script component)

Because it seems that it wants an object to be able to reference to something, but it can’t find it.

on July 7, 2017

I think yes ^^

Show more replies
  • Liked by
4 on July 4, 2017


on July 7, 2017

did you mean the formatting? (that got lost as I pasted it here…) sry


on July 7, 2017

No, he meant that your code should be on that subreddit cause your code is a horror…

on July 7, 2017

your answer is a horror, not helping and hurting!


on July 7, 2017

Untrue. His answer is correct, your code is a horror and is poorly structured. You should learn basic code structure beforehand. Dont get all butthurt, it happens to everyone.

Show more replies
  • Liked by
0 on July 12, 2017

You can start with checking if both saveFile and PlayerStats (along with other variables) are not null and Debug.Log message (or more meaningful exception) in case they are.

This way you will find out what is unreferenced object (aka which variable is null) and then you can try to figure our why.

  • Liked by
0 on July 18, 2017

im still confused… its weird that it worked before i added some floats… and i tryed with just the one float (money, i had this in before) and it still didnt work…

and its the saveFile.floatname what is unreferenced..

    public void LoadMoney() {
        Debug.Log ("It sould do something");
        if (SaveGameSystem.DoesSaveGameExist ("saveFile") == true) {
            Debug.Log ("There is a saveFile");
            MySaveGame saveFile = SaveGameSystem.LoadGame ("saveFile") as MySaveGame;
            Debug.Log ("saveFile Loaded");
            Debug.Log (PlayerStats.DirtFloat + " PlayerStats dirt float");
            Debug.Log (saveFile.DirtFloat + " saveFile dirt float");
//            PlayerStats.DirtFloat = saveFile.DirtFloat;
  • Liked by
4 on July 19, 2017

ererbe it would help us better help you if you pasted the entire script as some null ref errors are not on the line stated by unitys console but further back in the same script its just being thrown on that line 

on July 19, 2017

in this short script is the same error. why do you want to see the full script? (I added the MySaveGame.cs, maybe this is helpful…

and what i think is weird, that saving works 😐


if you want to see the full script(s), i put them on github ^^

on July 20, 2017

ok ill take a long look at them and ill tell you if i figure anything out 


on July 20, 2017

looking back at the first bit of the post i can see that in the gamestart code your not telling the script the name of the save file is this intentional? 


on July 20, 2017

after looking at the code ive found this

if (SaveGameSystem.DoesSaveGameExist("saveFile") == true) {
 MySaveGame saveFile = SaveGameSystem.LoadGame ("saveFile") as MySaveGame;

this is being called on

void Awake () {  
       loadAndSaveAGame.LoadTheGame ();

but what thats doing is when you load the game its looking for a save file, but you are making a new save file which will have all things set to null 


if (SaveGameSystem.DoesSaveGameExist("saveFile") == true) {
MySaveGame saveFile = SaveGameSystem.LoadGame ("saveFile") as MySaveGame;


if (SaveGameSystem.DoesSaveGameExist("saveFile") == false) {
MySaveGame saveFile = SaveGameSystem.LoadGame ("saveFile") as MySaveGame;
 PlayerStats.MoneyFloat = saveFile.MoneyFloat;
 PlayerStats.AppleFloat = saveFile.AppleFloat;
 PlayerStats.BreadFloat = saveFile.BreadFloat;
 PlayerStats.CharcoalFloat = saveFile.CharcoalFloat;
 PlayerStats.CheeseFloat = saveFile.CheeseFloat;
 PlayerStats.ClothFloat = saveFile.ClothFloat;
 PlayerStats.CopperIngotFloat = saveFile.CopperIngotFloat;
 PlayerStats.CopperOreFloat = saveFile.CopperOreFloat;
 PlayerStats.CornCobFloat = saveFile.CornCobFloat;
 PlayerStats.DirtFloat = saveFile.DirtFloat;
 PlayerStats.EggFloat = saveFile.EggFloat;
 PlayerStats.FeatherFloat = saveFile.FeatherFloat;
 PlayerStats.FishFloat = saveFile.FishFloat;
 PlayerStats.FleshFloat = saveFile.FleshFloat;
 PlayerStats.FlourFloat = saveFile.FlourFloat;
 PlayerStats.ForestFloat = saveFile.ForestFloat;
 PlayerStats.IronIngotFloat = saveFile.IronIngotFloat;
 PlayerStats.IronOreFloat = saveFile.IronOreFloat;
 PlayerStats.MilkFloat = saveFile.MilkFloat;
 PlayerStats.PealFloat = saveFile.PealFloat;
 PlayerStats.SaplingFloat = saveFile.SaplingFloat;
 PlayerStats.StoneFloat = saveFile.StoneFloat;
 PlayerStats.StoneCoalFloat = saveFile.StoneCoalFloat;
 PlayerStats.WaterFloat = saveFile.WaterFloat;
 PlayerStats.WheatFloat = saveFile.WheatFloat;
 PlayerStats.WoodLogFloat = saveFile.WoodLogFloat;
 PlayerStats.WoolFloat = saveFile.WoolFloat;
 PlayerStats.WheatFields = saveFile.WheatFields;
 PlayerStats.Wells = saveFile.Wells;
 PlayerStats.CharcoalClamps = saveFile.CharcoalClamps;
 PlayerStats.Windmills = saveFile.Windmills;
 PlayerStats.Bakerys = saveFile.Bakerys;
 PlayerStats.WoodcutterHuts = saveFile.WoodcutterHuts;
 PlayerStats.ForesterHuts = saveFile.ForesterHuts;
 PlayerStats.TreeNurserys = saveFile.TreeNurserys;
 PlayerStats.AppleTrees = saveFile.AppleTrees;
 PlayerStats.PealTrees = saveFile.PealTrees;
 PlayerStats.CornFields = saveFile.CornFields;
 PlayerStats.ChickenFarms = saveFile.ChickenFarms;
 PlayerStats.PigFarms = saveFile.PigFarms;
 PlayerStats.SheepFarms = saveFile.SheepFarms;
 PlayerStats.CowFarms = saveFile.CowFarms;
 PlayerStats.CheeseDairys = saveFile.CheeseDairys;
 PlayerStats.WeavingMills = saveFile.WeavingMills;
 PlayerStats.FisherHuts = saveFile.FisherHuts;
 PlayerStats.StoneMines = saveFile.StoneMines;
 PlayerStats.CoalMines = saveFile.CoalMines;
 PlayerStats.CopperMines = saveFile.CopperMines;
 PlayerStats.CopperFurnances = saveFile.CopperFurnances;
 PlayerStats.IronMines = saveFile.IronMines;
 PlayerStats.IronFurnances = saveFile.IronFurnances;
 //block Stats
 DirtBlock.DirtBlockLevel = saveFile.DirtBlockLevel;
 DirtBlock.DirtBonus = saveFile.DirtBonus;
 DirtBlock.PebbleBonus = saveFile.PebbleBonus;
 DirtBlock.posibilityDirtBonus = saveFile.posibilityDirtBonus;
 DirtBlock.posibilityPebble = saveFile.posibilityPebble;
 StoneBlock.StoneBlockLevel = saveFile.StoneBlockLevel;
 StoneBlock.StoneBonus = saveFile.StoneBonus;
 StoneBlock.posibilityStoneBonus = saveFile.posibilityStoneBonus;
 WoodLogBlock.WoodLogBlockLevel = saveFile.WoodLogBlockLevel;
 WoodLogBlock.WoodLogBonus = saveFile.WoodLogBonus;
 WoodLogBlock.posibilityWoodLogBonus = saveFile.posibilityWoodLogBonus;
 TerrariaBlock.TerrariaBlockLevel = saveFile.TerrariaBlockLevel;
 TerrariaBlock.TerrariaBonus = saveFile.TerrariaBonus;
 TerrariaBlock.posibilityTerrariaBonus = saveFile.posibilityTerrariaBonus;
 //block Costs
 CubeShopManager.DirtBlockCoinCost = saveFile.DirtBlockCoinCost;
 CubeShopManager.StoneBlockCoinCost = saveFile.StoneBlockCoinCost;
 CubeShopManager.WoodLogBlockCoinCost = saveFile.WoodLogBlockCoinCost;
 CubeShopManager.TerrariaBlockCoinCost = saveFile.TerrariaBlockCoinCost;
 PlayerStats.dirtPerClick = saveFile.dirtPerClick;
 PlayerStats.stonePerClick = saveFile.stonePerClick;
 PlayerStats.woodLogPerClick = saveFile.woodLogPerClick;
 PlayerStats.terrariaDirtPerClick = saveFile.terrariaDirtPerClick;
 PlayerStats.BlockClicks = saveFile.BlockClicks;

should fix the null ref but then you will then need to do your loading code p.s changed the last part so you could copy and paste it 

Show more replies
  • Liked by