Question by
danny1111 · Dec 28, 2015 at 06:02 PM ·
highscoreobject-reference-error
Object reference not set to an instance of an object
I don't really know what's wrong. Here's my code
//HighscoreScript
using UnityEngine;
using System.Collections;
public class HighscoreScript : MonoBehaviour {
const string privateCode = "private code";
const string publicCode = "548c04c96e51b60c740bcdec";
const string webURL = "http://dreamlo.com/lb/";
DisplayHighscores highscoreDisplay;
public static Highscore[] highscoresList;
static HighscoreScript instance;
void Awake() {
instance = this;
highscoreDisplay = GetComponent<DisplayHighscores> ();
}
public static void AddNewHighscore() {
Debug.Log ("Called");
instance.StartCoroutine(instance.UploadNewHighscore());
}
IEnumerator UploadNewHighscore() {
WWW www = new WWW(webURL + privateCode + "/add/" + WWW.EscapeURL(SystemInfo.deviceName) + "/" + SpeedController.score);
yield return www;
if (string.IsNullOrEmpty(www.error)) {
print ("Upload Successful");
DownloadHighscores();
}
else {
print ("Error uploading: " + www.error);
}
}
public void DownloadHighscores() {
StartCoroutine("DownloadHighscoresFromDatabase");
}
IEnumerator DownloadHighscoresFromDatabase() {
WWW www = new WWW(webURL + publicCode + "/pipe/");
yield return www;
if (string.IsNullOrEmpty (www.error)) {
FormatHighscores (www.text);
highscoreDisplay.OnHighscoresDownloaded(highscoresList);
}
else {
print ("Error Downloading: " + www.error);
}
}
void FormatHighscores(string textStream) {
string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
highscoresList = new Highscore[entries.Length];
for (int i = 0; i <entries.Length; i ++) {
string[] entryInfo = entries[i].Split(new char[] {'|'});
string username = entryInfo[0];
int score = int.Parse(entryInfo[1]);
highscoresList[i] = new Highscore(username,score);
}
}
}
public struct Highscore {
public string username;
public int score;
public Highscore(string _username, int _score) {
username = _username;
score = _score;
}
}
The error says NullReferenceException: Object reference not set to an instance of an object HighscoreScript.AddNewHighscore () (at Assets/Scripts/HighscoreScript.cs:25)
Comment
Your answer
