Burgzergarcade Why am i getting an error after re-importing?

I have been following Burgzergarcade’s tutorials and i had made a back up of my project to test something. And when I went to re-import it, everything seemed to work but when I play it I get an error.

This is the error:

NullReferenceException: Object reference not set to an instance of an object
GameSettings.LoadCharacterData () (at Assets/Scripts/GameSettings.cs:48)
GameMaster.LoadCharacter () (at Assets/Scripts/GameMaster.cs:61)
GameMaster.Start () (at Assets/Scripts/GameMaster.cs:47)

Here is my GameSettings.cs:

using UnityEngine;
using System.Collections;
using System;

public class GameSettings : MonoBehaviour {
	public const string PLAYER_SPAWN_POINT = "Player Spawn Point";		//this is the name of the gameObject that the player will spawn at at the start of the level
	
	void Awake(){
		DontDestroyOnLoad(this);										//allows script to surive from scene to scene
	}
	
	public void SaveCharacterData(){
		GameObject pc = GameObject.Find("pc");
		
		PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
		
		//PlayerPrefs.DeleteAll();		//use when adding or editing Vitals, Attributes, or Skills *Note: uncomment (Set - Mods when this is done)
		
		PlayerPrefs.SetString("Player Name", pcClass.Name);
		
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
			PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Base Value", pcClass.GetPrimaryAttribute(cnt).BaseValue);
			PlayerPrefs.SetInt(((AttributeName)cnt).ToString() + " - Exp To Level", pcClass.GetPrimaryAttribute(cnt).ExpToLevel);
		}
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
			PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Base Value", pcClass.GetVital(cnt).BaseValue);
			PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Exp To Level", pcClass.GetVital(cnt).ExpToLevel);
			PlayerPrefs.SetInt(((VitalName)cnt).ToString() + " - Cur Value", pcClass.GetVital(cnt).CurValue);
		
//			PlayerPrefs.SetString(((VitalName)cnt).ToString() + " - Mods", pcClass.GetVital(cnt).GetModifyingAttributeString());
			
		}
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
			PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Base Value", pcClass.GetSkill(cnt).BaseValue);
			PlayerPrefs.SetInt(((SkillName)cnt).ToString() + " - Exp To Level", pcClass.GetSkill(cnt).ExpToLevel);
		
//			PlayerPrefs.SetString(((SkillName)cnt).ToString() + " - Mods", pcClass.GetSkill(cnt).GetModifyingAttributeString());
						
		}
	
	}
		
	public void LoadCharacterData(){
		GameObject pc = GameObject.Find("pc");
		
		PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
		
		pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me");
		
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
			pcClass.GetPrimaryAttribute(cnt).BaseValue = PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Base Value", 0);
			pcClass.GetPrimaryAttribute(cnt).ExpToLevel	= PlayerPrefs.GetInt(((AttributeName)cnt).ToString() + " - Exp To Level", Attribute.STARTING_EXP_COST);
		}	
		
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
			pcClass.GetVital(cnt).BaseValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Base Value", 0);
			pcClass.GetVital(cnt).ExpToLevel = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Exp To Level", 0);
			
			//make sure you call this so that the AdjustedBaseValue will be updated before you try to call to get the curValue
			pcClass.GetVital(cnt).Update();
			
			//get the stored value for the curValue foreach vital
			pcClass.GetVital(cnt).CurValue = PlayerPrefs.GetInt(((VitalName)cnt).ToString() + " - Cur Value", 1);
		}
		
		for(int cnt = 0 ; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
			pcClass.GetSkill(cnt).BaseValue = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Base Value", 0);
			pcClass.GetSkill(cnt).ExpToLevel = PlayerPrefs.GetInt(((SkillName)cnt).ToString() + " - Exp To Level", 0);
				
		}
		//output the curValue for each of the vitals
		for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
//			Debug.Log(((SkillName)cnt).ToString() + ": " + pcClass.GetSkill(cnt).BaseValue + " - " + pcClass.GetSkill(cnt).ExpToLevel);
		}	
	}	
}

And here is my GameMaster.cs :

using UnityEngine;
using System.Collections;

public class GameMaster : MonoBehaviour {
	public GameObject playerCharacter;
	public GameObject gameSettings;
	public Camera mainCamera;
		
	public float zOffset;
	public float yOffset;	
	public float xRotOffset;
	
	private GameObject _pc;
	private PlayerCharacter _pcScript;
	
	public Vector3 _playerSpawnPointPos;			//this is the place where i want my player to spawn.
	
	// Use this for initialization
	void Start () {
//		_playerSpawnPointPos = new Vector3(466, 145, 720);	// the default positition for our player spawn point (used to be 1008, 66, 212)
		
		GameObject go = GameObject.Find(GameSettings.PLAYER_SPAWN_POINT);
		
		if(go == null){
			Debug.LogWarning("Can not find Player Spawn Point");	
			
			go = new GameObject(GameSettings.PLAYER_SPAWN_POINT);
			Debug.Log("Created Player Spawn Point");	
			
			go.transform.position = _playerSpawnPointPos;
			Debug.Log("Moved Player Spawn Point");	

		}	
		
		_pc = Instantiate(playerCharacter, go.transform.position, Quaternion.identity)as GameObject;
		_pc.name = "pc";
		
		_pcScript =_pc.GetComponent<PlayerCharacter>();
		
		zOffset = -2.5f;
		yOffset = 2.5f;
		xRotOffset = 22.5f;
		
		mainCamera.transform.position = new Vector3(_pc.transform.position.x, _pc.transform.position.y + yOffset, _pc.transform.position.z + zOffset);
		mainCamera.transform.Rotate(xRotOffset, 0, 0);
	
		LoadCharacter();
	}
	
	public void LoadCharacter(){
		GameObject gs = GameObject.Find("__GameSettings");
		
		if(gs = null){
			GameObject gs1 = Instantiate(gameSettings, Vector3.zero, Quaternion.identity) as GameObject;
			gs1.name = "__GameSettings";
			
		}
		GameSettings gsScript = GameObject.Find("__GameSettings").GetComponent<GameSettings>();
			
		//load the character data
		gsScript.LoadCharacterData();	
	}
}

If you see any errors in my script or if you need more info let me know.

Thank You.

What you need to do is look at the lines that the error is telling you, and see which object is null that shouldn’t be. Anytime you do a method on an object, like pc.GetComponent<>(), if pc is null, you will get that error. Anyway, if you debug into your game you will probably see which object is null, and then you can look at why it’s not getting set correctly.

So, just looking at your code what it’s telling you is that in the GameSettings.LoadCharacterData method, on line 48, there was a null object reference error, like I described above. If you look at that method in GameSettings you see:

    public void LoadCharacterData(){
       GameObject pc = GameObject.Find("pc");

       PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();

       pcClass.Name = PlayerPrefs.GetString("Player Name", "Name Me"); <-- line 48

       for(int cnt = 0 ; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){

There are two object methods getting called in that line. pcClass.Name and PlayerPrefs.GameString(. Either pcClass or PlayerPrefs is null, which means it’s not getting set up correctly anymore. You need to debug in and find out why.

Once you do that, remember this process, because you’ll have to do it a million times. Welcome to Programming :slight_smile: