Search Unity

how to work with custom classes that have arrays of custom classes within

Discussion in 'Scripting' started by unit3ro, Dec 18, 2015.

  1. unit3ro

    unit3ro

    Joined:
    Dec 18, 2014
    Posts:
    15
    I made a script that generates items from a file, then these items are stored in different books but I get errors when creating a new book and try to store an item in.

    what am I doing wrong??

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. //=================================//
    7. public class BooksGenerator3: MonoBehaviour {
    8.    
    9.     public TextAsset TextData = null;
    10.     private string[] strData; // = "item1+102+M7,M8,M9,I15,I16,I17,I18,J8,J9,J10/item2+103+I14,I15,I16,I17,V14,V15,V16,V17,V18/";
    11.    
    12.     [System.Serializable]
    13.     public class ITEM {      
    14.         public string[] itemData = new string[2];
    15.         public string[] words;
    16.     }
    17.     [System.Serializable]
    18.     public class BOOK {      
    19.         public ITEM[] items;
    20.     }
    21.    
    22.     public BOOK[] inventory;
    23.     public ITEM[] items;
    24.    
    25.     //=================================//
    26.     void Start () {
    27.        
    28.         strData = TextData.text.Split(new char[] {'/'});
    29.         items = processItems(strData);
    30.         inventory = generateBooks(items);
    31.        
    32.         for(int x=0; x<inventory.Length; x++) {
    33.            
    34.             print("curBook: " + inventory[x].ITEM.itemData);
    35.         }
    36.        
    37.     }
    38.     //=================================//
    39.     public ITEM[] processItems(string[] strData)  {
    40.        
    41.         ITEM[] tempItems = new ITEM[strData.Length];      
    42.        
    43.         for(int s=0; s<strData.Length; s++) {
    44.             string[] curData = strData[s].Split(new char[] {'+'});
    45.        
    46.             ITEM newItem = new ITEM();
    47.             newItem.itemData[0] = curData[0];
    48.             newItem.itemData[1] = curData[1];
    49.             newItem.words = curData[2].Split(new char[] {','});
    50.             tempItems[s] = newItem;
    51.            
    52.         }
    53.         return tempItems;
    54.     }
    55.     //=================================//
    56.     public BOOK[] generateBooks(ITEM[] Items) {
    57.  
    58.         ArrayList firstBooks = new ArrayList();
    59.  
    60.         for(int i=0; i<Items.Length; i++) {          
    61.             BOOK tempBook = new BOOK();
    62.             tempBook.items[i] = Items[i];
    63.             firstBooks.Add(tempBook);
    64.         }
    65.         return firstBooks.ToArray(typeof(BOOK)) as BOOK[];  
    66.     }
    67.  
    68. }
    69.  
    and the file contains this
    with this file two items are created with your data