class and array issue

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

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

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.

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);
		}
	}
	
	
}

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.

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

Put it after the “score = new Highscore[10];” line.

o WOW thank you so MUCH!!!

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

Because structs and primitive types have a default value, and classes do not.

–Eric

Thanks Eric going to google the dif types:)