Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kblood · Sep 10, 2014 at 05:26 PM · class

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zombience · Sep 11, 2014 at 06:53 PM 0
Share

@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;
             }
avatar image Gkupce · Sep 12, 2014 at 01:18 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How to sum a property of a class in a List 2 Answers

Using an enum in a class constructor. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges