Currently making a scoreboard application and I ran into a NULLReferenceException:
NullReferenceException: Object reference not set to an instance of an object
Scoreboard.Update () (at Assets/Scripts/Scoreboard.cs:20)
Scoreboard.cs
using UnityEngine;
using System.Collections;
public class Scoreboard : MonoBehaviour {
GameScore gs = new GameScore();
GameScore[] gameScores = new GameScore[6];
void Awake(){
gs = GetComponent<GameScore>();
}
// Use this for initialization
void Start () {
Debug.Log(gs.myScore.gameNum.ToString());
}
// Update is called once per frame
void Update () {
}
}
GameScore.cs
using UnityEngine;
using System.Collections;
public class GameScore : MonoBehaviour {
public class Score
{
public int gameNum;
public string gameType;
public int RedScore;
public int BlueScore;
public Score(int gn, string gt, int rs, int bs)
{
gameNum = gn;
gameType = gt;
RedScore = rs;
BlueScore = bs;
}
// Constructor
public Score ()
{
gameNum = 1;
gameType = "NULL";
RedScore = 0;
BlueScore = 0;
//Debug.Log("Constructed Score");
}
}
public Score myScore = new Score();
void Start ()
{
Debug.Log(myScore.gameNum);
}
}
Not really sure where the bug could be. The Awake() function instantiates it, and none of the variables should be left un-initialized. Confused.