1010
December 1, 2012, 8:11pm
1
If someone could explain the error of my way’s, it would be greatly appreciated.
class Highscore{
public string name;
public int deaths;
public int kills;
}
// Use this for initialization
void Start () {
score = new Highscore[10];
Highscore[0].name = "Ron";
Highscore[1].name = "John";
Highscore[2].name = "Smith";
Highscore[3].name = "Josh";
Highscore[4]. name = "Sam";
Highscore[5].name = "Kat";
Highscore[6].name = "Justin";
Highscore[7].name = "Ray";
Highscore[8].name = "Zack";
Highscore[9].name = "Nicole";
}
}
You need a variable name; “Highscore” is the name of the class so you can’t use that. Also, http://forum.unity3d.com/threads/143875-Using-code-tags-properly
–Eric
1010
December 1, 2012, 9:27pm
3
How would I assign this var so it will work in my OnGUI function also.
class Highscore{
public string name;
public int deaths;
public int kills;
}
// Use this for initialization
void Start () {
var score = new Highscore[10];
score[0].name = "Ron";
score[1].name = "John";
score[2].name = "Smith";
score[3].name = "Josh";
score[4].name = "Sam";
score[5].name = "Kat";
score[6].name = "Justin";
score[7].name = "Ray";
score[8].name = "Zack";
score[9].name = "Nicole";
}
void OnGUI()
{
for( int i = 0; i < 10; i++){
GUI.Box(new Rect( Screen.width /2,
Screen.height/2,
250, 50), score[i]);
}
}
Make it a global variable (outside functions).
–Eric
1010
December 1, 2012, 9:55pm
5
I’m sorry I guess I don’t mean how I mean what would I assign it (float int string)i’m not sure. Also thanks so much for helping me.
You would use:
Highscore[] score;
void Start()
{
score = new Highscore[10];
// etc.
}
Also check out this C# Crash Course to learn more.
1010
December 3, 2012, 12:20am
7
thanks Eddy but, now I’m getting NullReferenceException: Object reference not set to an instance of an object
HighscoreDisplay.OnGUI ()
using UnityEngine;
using System.Collections;
public class HighscoreDisplay : MonoBehaviour {
Highscore[] score;
class Highscore{
public string name;
public int deaths;
public int kills;
}
// Use this for initialization
void Start () {
score = new Highscore[10];
score[0].name = "Ron";
score[1].name = "John";
score[2].name = "Smith";
score[3].name = "Josh";
score[4].name = "Sam";
score[5].name = "Kat";
score[6].name = "Justin";
score[7].name = "Ray";
score[8].name = "Zack";
score[9].name = "Nicole";
}
void OnGUI()
{
for( int i = 0; i < 10; i++){
GUI.Box(new Rect( Screen.width /2,
Screen.height/2,
250, 50), score[i].name);
}
}
}
Zenix
December 3, 2012, 3:53am
8
The line "score = new Highscore[10]; " initializes the array, you still need to initialize each instance of Highscore.
for( int i = 0; i < 10; i++ ){ score[i] = new Highscore(); }
You’ll also want to space out your display of them, right now you’re creating each GUI.Box in the exact same location… you’ll want to use the ‘i’ variable in there.
1010
December 3, 2012, 4:31am
9
i’m sorry I understand the 2nd part of what your trying to tell me but not the 1st. not sure what to do with that one linner
Zenix
December 3, 2012, 4:32am
10
Put it after the “score = new Highscore[10];” line.
1010
December 3, 2012, 4:36am
11
o WOW thank you so MUCH!!!
1010
December 3, 2012, 4:54am
12
I am so confused right now…with
public string[] score;
score = new string[10];
score[0].name = "Ron";
score[1].name = "John";
score[2].name = "Smith";
score[3].name = "Josh";
score[4].name = "Sam";
score[5].name = "Kat";
score[6].name = "Justin";
score[7].name = "Ray";
score[8].name = "Zack";
score[9].name = "Nicole";
How come I dont need the forLoop with the string but, with Highscore class added I do
Eric5h5
December 3, 2012, 5:05am
13
Because structs and primitive types have a default value, and classes do not.
–Eric
1010
December 3, 2012, 5:14am
14
Thanks Eric going to google the dif types:)