unity3d - Cannot access array of a custom class -


in unity keep getting error message "nullreferenceexception: object reference not set instance of object" on this:

listofbanks[0].deposit(50);

and

accntblnce.text = "account balance:\n" + listofbanks[curbank].getbalance().tostring("c");

i have 3 options listed in drop down menu , when debug.log number of items in array 3 count. can't them. banks variable set dropdown object in inspector accntblnce text object in panel.

the code below.

banks.cs

public class banks : monobehaviour {      public dropdown banks;     public text accntblnce;     public bank[] listofbanks;     public int curbank = 0;      void start() {         listofbanks = new bank[banks.options.count];         listofbanks[0].deposit(50);     }      void update() {         curbank = banks.value;         accntblnce.text = "account balance:\n" + listofbanks[curbank].getbalance().tostring("c");     } } 

bank.cs

public class bank{      public bank() { }      public bank(string orgn, float amnttorprt, float blnce) {         origin = orgn;         amounttoreport = amnttorprt;         balance = blnce;     }      public string origin { get; set; }     public float amounttoreport { get; set; }     public float balance { get; set; }      public bool deposit(float amnt) {         if (amnt > 0) {             balance += amnt;             if(amnt > amounttoreport) {                 flagforreport();             }             return true;         }         else             return false;     }      private void flagforreport() {         throw new notimplementedexception();     }      public float getbalance() {         return balance;     }      public bool withdraw(float amnt) {         if (amnt > 0) {             if (balance >= amnt) {                 balance -= amnt;                 return true;             }             else                 return false;         }         else             return false;     }      public bool transfer(float amnt, bank bank) {         if (amnt > 0) {             if (balance >= amnt) {                 if(bank.deposit(amnt))                     balance -= amnt;                 return true;             }             else                 return false;         }         else             return false;     } } 

this fourth time array question asked week the-same problem , the-same solution.

you declared array here:

listofbanks = new bank[banks.options.count]; 

but did not create new instance of each bank script before calling

listofbanks[0].deposit(50); , listofbanks[curbank].getbalance().tostring("c").

declaring array , setting size not the-same creating new instance of script.

the solution loop through array , create new instance of each one.

in banks.cs, replace code in start() function 1 below:

void start() {     //declare how bank array should created     listofbanks = new bank[banks.options.count];      //now create instance of each bank     (int = 0; < listofbanks.length; i++)     {         //create new instance of each bank class         //listofbanks[i] = new bank();         listofbanks[i] = new bank("", 50, 50);     }      listofbanks[0].deposit(50); } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo