- Home /
How to initialize a class array in C#?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LandeData : MonoBehaviour {
public Land[] lande;
// Use this for initialization
void Start ()
{
lande[0] = new Land("Danmark", 100, 100, 100, 0, 5.5f, 35);
lande[1] = new Land("Amerika", 80, 80, 80, 0, 250, 50);
}
[System.Serializable]
public class Land
{
public string navn;
public int moral;
public int håb;
public int helbred;
public int indflydelse;
public float indbyggere;
public int forurening;
public int relationDanmark;
public int relationAmerika;
public int relationRusland;
public int relationIran;
public int relationKina;
public int relationSaudArab;
public int relationEngland;
public int relationIsrael;
public Land(string nav, int mor, int hå, int helb, int indf, float indb, int foruren)
{
navn = nav;
moral = mor;
håb = hå;
helbred = helb;
indflydelse = indf;
indbyggere = indb;
forurening = foruren;
}
}
// Update is called once per frame
void Update ()
{
gameObject.GetComponent<Text>().text = (lande[0].navn + "\t" + lande[0].moral +"\t\t"+ lande[0].håb +"\t\t" + lande[0].indbyggere + "\t\t\t" + lande[0].forurening + "\n\n");
GetComponent<Text>().text = (GetComponent<Text>().text + lande[1].navn + "\t" + lande[1].moral +"\t\t"+ lande[1].håb +"\t\t" + lande[1].indbyggere + "\t\t\t" + lande[1].forurening + "\n\n");
}}
I am trying to make this work:
But it comes with the error:
IndexOutOfRangeException: Array index is out of range.
(wrapper stelemref) object:stelemref (object,intptr,object)
LandeData.Start () (at Assets/aMyStuff/Organiser Jorden/Scripts/LandeData.cs:12)
How do I make the array accessible?
Answer by zombience · Sep 10, 2014 at 05:35 PM
you need to initialize the array before you can populate the indices of the array with instances of classes.
in your code above, lande is actually null, because you have not initialized the array yet.
you need to do that first, then assign each index. like so:
int numberOfLands = 2;
void Start ()
{
lande = new Land[numberOfLands]; // create an array long enough for as many lands as you need to store
lande[0] = new Land("Danmark", 100, 100, 100, 0, 5.5f, 35);
lande[1] = new Land("Amerika", 80, 80, 80, 0, 250, 50);
}
Answer by Gkupce · Sep 10, 2014 at 07:24 PM
Arrays in C# are somewhat objects, so you have to ask for the memory space for it before assigning to it
AnyType[] array = new AnyType[arraySize];
In your case:
void Start ()
{
lande = new Land[size];
//you'd need at least size = 2 for the following lines to work
lande[0] = new Land("Danmark", 100, 100, 100, 0, 5.5f, 35);
lande[1] = new Land("Amerika", 80, 80, 80, 0, 250, 50);
}
If instead what you want is to be able to access the array from the editor it wouldn't make much sense to overwrite them on start, much less not checking it's Length to make sure it can take the data. Also if you want to use an array initialized by the editor on OnDrawGizmos or OnDrawGizmosSelected make sure to check that the array is not null and its length greater than 0.
@kblood included a constructor in the class, that isn't the issue. the constructor in that class is adequate, in lines 36 to 45:
public Land(string nav, int mor, int hå, int helb, int indf, float indb, int foruren)
{
navn = nav;
moral = mor;
håb = hå;
helbred = helb;
indflydelse = indf;
indbyggere = indb;
forurening = foruren;
}
What i meant was that it needed a constructor without parameters, but after testing it shouldn't be needed, still the checks for the vector to not be null, and to be big enough to hold the data should be made.
Your answer
Follow this Question
Related Questions
Null reference when trying to return an object 2 Answers
Set Dirty on class instance? 0 Answers
Don't Understand Error 2 Answers