Hello,
i have simple time counter, which stop when my game object is destroyed.
Timer.cs
public class Timer : MonoBehaviour {
//Timerio kintamieji
public float startTimeT = 0;
private string currentTime;
//Gui skinai
public GUISkin skin;
private bool timeUpdate = false;
void Start(){
timeUpdate = true;
}
void Update(){
if(timeUpdate){
startTimeT += Time.deltaTime;
currentTime = string.Format ("{0:0.00}", startTimeT);
}
}
public void GameOver(){
timeUpdate = false;
}
void OnGUI(){
GUI.skin = skin;
//Timerio tiksejimas ir spalvos
GUI.TextField (new Rect(Screen.width - 150,Screen.height - 100, 150, 100), "Time:
" + currentTime);
}
}
So I am creating a leaderBoard, which shows up when player is destroyed.
My score is equal to seconds which is counted in Timer.cs.
My question is how to show score results in GameOverScript.cs. Now I am getting such an error: NullReferenceException: Object reference not set to an instance of an object
GameOverScript.cs
public class GameOverScript : MonoBehaviour
{
private GUISkin skin;
int totalScore = 0;
string playerName = "";
enum gameState {
waiting,
running,
enterscore,
leaderboard
};
gameState gs;
dreamloLeaderBoard dl;
dreamloPromoCode pc;
public Timer timer;
void Start()
{
// get the reference here...
this.dl = dreamloLeaderBoard.GetSceneDreamloLeaderboard();
// get the other reference here
this.pc = dreamloPromoCode.GetSceneDreamloPromoCode();
this.gs = gameState.waiting;
//(Timer)this.GetComponent(typeof(Timer));
timer = timer.GetComponent<Timer>();
totalScore = (int) timer.startTimeT;
//totalScore = 10;
print (totalScore);
skin = Resources.Load("GUISkin") as GUISkin;
}
void OnGUI()
{
const int buttonWidth = 140;
const int buttonHeight = 60;
GUI.skin = skin;
if (GUI.Button(
// Center in X, 1/3 of the height in Y
new Rect(Screen.width / 2 - (buttonWidth / 2), (1 * Screen.height / 3) - (buttonHeight / 2), buttonWidth, buttonHeight),
"RETRY"
))
{
// Reload the level
Application.LoadLevel("Stage1");
}
if (GUI.Button(
// Center in X, 2/3 of the height in Y
new Rect(Screen.width / 2 - (buttonWidth / 2), (2 * Screen.height / 3) - (buttonHeight / 2), buttonWidth, buttonHeight),
"BACK"
))
{
// Reload the level
Application.LoadLevel("Menu");
}
GUILayoutOption[] width200 = new GUILayoutOption[] {GUILayout.Width(300)};
float width = 400; // Make this wider to add more columns
float height = 200;
//GUI.Box (new Rect (Screen.width - 100,0,100,50)
Rect r = (new Rect (Screen.width - 100,0,200,100));
GUILayout.BeginArea(r, new GUIStyle("box"));
GUILayout.BeginVertical();
if (this.gs == gameState.waiting || this.gs == gameState.running)
{
GUILayout.Label("Total Score: " + this.totalScore.ToString());
GUILayout.BeginHorizontal();
GUILayout.Label("Your Name: ");
this.playerName = GUILayout.TextField(this.playerName, width200);
if (GUILayout.Button("Save Score"))
{
// add the score...
if (dl.publicCode == "") Debug.LogError("You forgot to set the publicCode variable");
if (dl.privateCode == "") Debug.LogError("You forgot to set the privateCode variable");
dl.AddScore(this.playerName, totalScore);
this.gs = gameState.leaderboard;
}
GUILayout.EndHorizontal();
}
if (this.gs == gameState.leaderboard)
{
GUILayout.Label("High Scores:");
List<dreamloLeaderBoard.Score> scoreList = dl.ToListHighToLow();
if (scoreList == null)
{
GUILayout.Label("(loading...)");
}
else
{
int maxToDisplay = 20;
int count = 0;
foreach (dreamloLeaderBoard.Score currentScore in scoreList)
{
count++;
GUILayout.BeginHorizontal();
GUILayout.Label(currentScore.playerName, width200);
GUILayout.Label(currentScore.score.ToString(), width200);
GUILayout.EndHorizontal();
if (count >= maxToDisplay) break;
}
}
}
GUILayout.EndArea();
}
}