- Home /
NullReferenceException script problem?
here is a video to help explain what i am talking about
then here is the code for the first script that is used to call Gameset..
using UnityEngine; using System.Collections; using System; //used for the Enum class
public class CharGen: MonoBehaviour { private PlayerChar _toon; private const int STARTING_POINTS = 350; private const int MIN_STARTING_ATTRIBUTE_VALUE = 10; private const int START_VALUE = 50; private int pointsLeft;
private const int OFFSET = 5;
private const int LINE_HIEGHT = 20;
private const int STAT_LABEL_WIDTH = 100;
private const int BASEVALUE_LABEL_WIDTH = 30;
private const int BUTTON_WIDTH = 20;
private const int BUTTON_HIEGHT = 20;
private int statStartingPos = 40;
public GUISkin mySkin;
public GameObject playerPrefab;
// Use this for initialization
void Start () {
GameObject pc = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity) as GameObject;
pc.name = "playerchar";
// _toon = new PlayerChar(); // _toon.Awake();
_toon = pc.GetComponent<PlayerChar>();
pointsLeft = STARTING_POINTS;
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
_toon.GetPrimaryAttribute(cnt).BaseValue = START_VALUE;
pointsLeft -= (START_VALUE - MIN_STARTING_ATTRIBUTE_VALUE);
}
_toon.StatUpdate();
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
GUI.skin = mySkin;
DisplayName();
DisplayPointsLeft();
DisplayAttribute();
DisplayVital();
DisplaySkill();
DisplayCreateButton();
}
private void DisplayName() {
GUI.Label(new Rect(10, 10, 50, 25), "Name");
_toon.Name = GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
}
private void DisplayAttribute() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
GUI.Label(new Rect( OFFSET, //z
statStartingPos + (cnt * LINE_HIEGHT), //y
STAT_LABEL_WIDTH, //width
LINE_HIEGHT //height
), ((AttributeName)cnt).ToString());
GUI.Label(new Rect( STAT_LABEL_WIDTH + OFFSET, //x
statStartingPos + (cnt * LINE_HIEGHT), //y
BASEVALUE_LABEL_WIDTH, //width
LINE_HIEGHT //height
), _toon.GetPrimaryAttribute(cnt).AddjustedBaseValue.ToString());
if(GUI.Button(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH, //x
statStartingPos + (cnt * BUTTON_HIEGHT), //y
BUTTON_WIDTH, //width
BUTTON_HIEGHT //height
), "-")) {
if( _toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
_toon.GetPrimaryAttribute(cnt).BaseValue--;
pointsLeft++;
_toon.StatUpdate();
}
}
if(GUI.Button(new Rect( OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH, //x
statStartingPos + (cnt * BUTTON_HIEGHT ), //y
BUTTON_WIDTH, //width
BUTTON_HIEGHT //height
), "+")) {
if(pointsLeft > 0) {
_toon.GetPrimaryAttribute(cnt).BaseValue++;
pointsLeft--;
_toon.StatUpdate();
}
}
}
}
private void DisplayVital() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++) {
GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 8) * LINE_HIEGHT), STAT_LABEL_WIDTH, LINE_HIEGHT), ((VitalName)cnt).ToString());
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 8) * LINE_HIEGHT), BASEVALUE_LABEL_WIDTH, LINE_HIEGHT), _toon.GetVital(cnt).AddjustedBaseValue.ToString());
}
}
private void DisplaySkill() {
for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++) {
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2, statStartingPos + (cnt * LINE_HIEGHT), STAT_LABEL_WIDTH, LINE_HIEGHT), ((SkillName)cnt).ToString());
GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2 + STAT_LABEL_WIDTH, statStartingPos + (cnt * LINE_HIEGHT), BASEVALUE_LABEL_WIDTH, LINE_HIEGHT), _toon.GetSkill(cnt).AddjustedBaseValue.ToString());
}
}
private void DisplayPointsLeft() {
GUI.Label(new Rect(250, 10, 100, 25), "Points Left:" + pointsLeft.ToString());
}
private void DisplayCreateButton() {
if(GUI.Button(new Rect(Screen.width/ 2 - 50, statStartingPos + (11 * LINE_HIEGHT), 100, LINE_HIEGHT), "Create")) {
GameObject gs = GameObject.Find("__gamesetting");
Gameset gsScript = gs.GetComponent<Gameset>();
gsScript.SaveCharData();
Application.LoadLevel("Souls of Blades");
}
}
}
then here is the script Gameset
using UnityEngine; using System.Collections; using System;
public class Gameset : MonoBehaviour {
void awake() {
DontDestroyOnLoad(this);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void SaveCharData() {
GameObject pc = GameObject.Find("pc");
PlayerChar pcClass = pc.GetComponent<PlayerChar>();
PlayerPrefs.SetString("Player Name", pcClass.Name);
for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
PlayerPrefs.SetInt(((AttributeName)cnt).ToString(), pcClass.GetPrimaryAttribute(cnt).BaseValue);
}
}
public void LoadCharData() {
}
}
Don't post a grainy video and expect us to know what you're talking about. You should actually take the time to write out and explain exactly what your problem is. $$anonymous$$aybe it will become clear to you while you do that what is going on!
Also, don't just post your entire codebase here- only post the bits that are actually relevant. We don't need to know that your classes inherit from $$anonymous$$onoBehaviour, and import UnityEngine. That's usually assumed to be the case, unless stated otherwise. So don't post it! Usually problems arise in just one or two lines, or interactions between a few lines- not the entire script.