- Home /
Reference error -_- Annoyed please help
Okay so I am following a tutorial and yet again in the tutorial the guy jumped past the part where he fixed an error and now i am stuck trying to solve it. I am stuck and can't seem to figure out the next step on how to fix this error. (scripting is not my favorite) Any help is appreciated thanks. Here the errors:
NullReferenceException: Object reference not set to an instance of an object GameSettings.SaveCharacterData () (at Assets/Game Data/Player Data/Scripts/Character Stats/GameSettings.cs:24) CharacterGenerator.DisplayCreateButton () (at Assets/Game Data/Player Data/Scripts/Character Stats/CharacterGenerator.cs:116) CharacterGenerator.OnGUI () (at Assets/Game Data/Player Data/Scripts/Character Stats/CharacterGenerator.cs:52)
Heres my Codes.
CHARACTERGENERATOR
using UnityEngine;
using System.Collections;
using System;
public class CharacterGenerator : MonoBehaviour
{
private PlayerCharacter _player;
private const int Talent_Points = 25;
private const int Minimum_Starting_Attribute_Value = 15;
private const int Starting_Value = 25;
private int pointsLeft;
public Texture2D cgTexture;
public GUIStyle nbStyle;
public GUIStyle pbStyle;
public GUIStyle bgStyle;
public GUIStyle tbStyle;
public GUISkin gameSkin;
public GameObject playerPrefab;
void Start()
{
GameObject pc = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
pc.name = "Character";
_player = pc.GetComponent<PlayerCharacter>();
_player.Awake();
pointsLeft = Talent_Points;
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)
{
_player.GetPrimaryAttributes(cnt).BaseValue = Minimum_Starting_Attribute_Value;
}
_player.StatUpdate();
}
void Update()
{
//_player.StatUpdate();
}
void OnGUI()
{
GUI.skin = gameSkin;
DisplayName();
DisplayPointsLeft();
DisplayAttributes();
DisplayVitals();
DisplaySkills();
DisplayCreateButton();
}
private void DisplayName()
{
GUI.Label(new Rect(10, 10, 50, 25), "Name:");
_player.Name = GUI.TextField(new Rect(65, 10, 250, 50), _player.Name, 14);
}
private void DisplayAttributes()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)
{
GUI.Label (new Rect(10, 75 +(cnt * 30), 100, 25), ((AttributeName)cnt).ToString() );
GUI.Label (new Rect(115, 75 +(cnt * 30), 30, 25), _player.GetPrimaryAttributes(cnt).AdjustedBaseValue.ToString() );
if(GUI.Button(new Rect(165, 75 + (cnt * 30), 25, 25), " ", nbStyle))
{
if(_player.GetPrimaryAttributes(cnt).BaseValue > Minimum_Starting_Attribute_Value)
{
_player.GetPrimaryAttributes(cnt).BaseValue--;
pointsLeft ++;
_player.StatUpdate();
}
}
if(GUI.Button(new Rect(195, 75 + (cnt * 30), 25, 25), " ", pbStyle))
{
if(pointsLeft > 0)
{
_player.GetPrimaryAttributes(cnt).BaseValue++;
pointsLeft --;
_player.StatUpdate();
}
}
}
}
private void DisplayVitals()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++)
{
GUI.Label (new Rect(225, 75 +(cnt * 40), 100, 25), ((VitalName)cnt).ToString() );
GUI.Label (new Rect(340, 75 +(cnt * 40), 30, 25), _player.GetVitals (cnt).AdjustedBaseValue.ToString() );
}
}
private void DisplaySkills()
{
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++)
{
GUI.Label (new Rect(415, 75 +(cnt * 40), 100, 25), ((SkillName)cnt).ToString() );
GUI.Label (new Rect(530, 75 +(cnt * 40), 30, 25), _player.GetSkills (cnt).AdjustedBaseValue.ToString() );
}
}
private void DisplayPointsLeft()
{
GUI.Label(new Rect(350, 10, 125, 25), "Points Left: " + pointsLeft.ToString());
}
private void DisplayCreateButton()
{
if(GUI.Button(new Rect(100, 500, 125, 35), "Create"))
{
GameSettings gsScript = GameObject.Find ("GameSettings").GetComponent<GameSettings>();
gsScript.SaveCharacterData();
Application.LoadLevel("DireQuest Game");
}
}
}
GAMESETTINGS
using UnityEngine;
using System.Collections;
public class GameSettings : MonoBehaviour {
// Use this for initialization
void Awake()
{
DontDestroyOnLoad(this);
}
void Start () {
}
// Update is called once per frame
void Update () {
}
public void SaveCharacterData()
{
GameObject pc = GameObject.Find("pc");
PlayerCharacter pcClass = pc.GetComponent<PlayerCharacter>();
PlayerPrefs.SetString("PlayerName", pcClass.Name);
}
public void LoadCharacterData()
{
}
}
Sorry for the length. Thanks again!
Answer by VesuvianPrime · Sep 14, 2014 at 08:27 PM
GameObject.Find("pc"); is returning null
Which means, in other words, that you don"'t have an object in your scene called "pc".
Your answer
![](https://koobas.hobune.stream/wayback/20220613163009im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Random exception error just showed up 0 Answers
Sync with visual studio 2010 error 1 Answer
,SUCCEDED(HR) Error 0 Answers
WeaponScript not working 0 Answers