2D ARRAY containing int, float and string values

I’m looking for a way to create, load and read a 2D array containing ints, floats and strings. Problem is I can’t read the values I’ve entered.

For each type of object I would want the corresponding row of the array to contain, type, position (X, Y, Z), address to texture, address to sounds, other atributes). That means ArrayList?

Here’s my trial and error into the arrays (OKs, NOGOs) . What works in 1D doesn’t work on 2D.
A GUI text displays the results onscreen.

Tried and read on the subject on the net but I just can’t crack it. I dont mind writing by hand the 2D array in the script (for starters, at least).

Thanks.


using UnityEngine;
using System.Collections;

public class scripttoread : MonoBehaviour
{
public GUIText textul;

	public int[] matrice_numere;
	public string[] matrice_stringuri;
	public int[, ] matr_1;
	public string[,] matr_2;
	public string ceva, a, b ;
	public int c, d	;
	public ArrayList arrayList_1 = new ArrayList ();
	
	void Start ()
	{
			matrice_numere = new int[5];
			matrice_numere [0] = 1;
			matrice_numere [1] = 2;
			matrice_numere [2] = 3;
			matrice_numere [3] = 4;
			matrice_numere [4] = 5;
			int lungime_matrice = matrice_numere.Length;
			Debug.Log (lungime_matrice);
			
			matrice_stringuri = new string[5];
			matrice_stringuri [0] = "Jan.";
			matrice_stringuri [1] = "Feb. ";
			matrice_stringuri [2] = "Mar. ";
			matrice_stringuri [3] = "Apr. ";
					
			arrayList_1.Add (5);
			arrayList_1 [0] = "1";
			arrayList_1 [1] = " January";
			arrayList_1 [2] = "is the first";
			arrayList_1 [3] = "day ";
			arrayList_1 [4] = " of";
			arrayList_1 [5] = 2015;
	
			matr_1 = new int[10, 10];
			matr_1 [1, 1] = 100;
			matr_1 [1, 2] = 200;
	
			matr_2 = new string[10, 10];
			matr_2 [1, 1] = "Ianuarie";
			matr_2 [1, 2] = "2014";
	}


	void Update ()
	{
			//textul.text = matrice_stringuri [3]; //OK
			//textul.text = arrayList_1 [1].ToString () + "ceva";	//NO GO
			
			//ceva = string.Concat ("ala", "bala");//OK
			//textul.text = ceva; //OK
			
			//textul.text = string.Concat ("ala", "bala"); //OK
			
			a = matrice_stringuri [1];
			b = matrice_stringuri [2];
			//ceva = String.Concat (a, b);	// NO GO
			//textul.text = a + b;	// Okkkkkk

				
			//textul.text = arrayList_1 [0] + arrayList_1 [1];	//NO GO
			
			//textul.text = matr_2 [1, 1].ToString (); //NO GO
	
			//textul.text = matrice_stringuri [2]; //OK
			
			//textul.text = matr_2 [1, 1]; NO GO
	
	
			c = matr_1 [1, 1];
			d = matr_1 [1, 2];
			textul.text = matr_1 [1, 1];//NO GO		
			//textul.text = c; //NO GO
			//textul.text = c.ToString; // NO GO
			//textul.text = c + d; //NO GO
	
	
	}

}

NO, NO, NO!

There is no reason to use an ArrayList. Not ever. The class has been completely superseded and is only in the .NET specification for backwards compatibility.

This is a major problem with your architecture. Ultimately the code you are trying to write will get you into trouble, even if you can pull it off without errors. Its not extendable, and will be a incredibly difficult to maintain.

Here is some pseudo code to show you how you should structure your code. Keep all of your data in one class, and manipulate it as you desire. Building multiple arrays and trying to maintain the same indexing on each is asking for trouble.

using UnityEngine; 
using System.Collections;
using System.Collections.Generic;

public class ScriptToRead : MonoBehaviour {
    public DataClass[,] data = new DataClass[10,10];

    void Start (){
        data[0,0] = new DataClass();
        data[0,0].strings.Add("What");
        data[0,0].strings.Add("was");
        data[0,0].strings.Add("I");
        data[0,0].strings.Add("thinking");
        data[0,0].strings.Add("?");

        data[0,0].position = new Vector3(1,4,-17);
    }

}

public class DataClass {
    public List<string> strings = new List<string>();
    public Vector3 position;
    public ....;
}

Get the idea? Let me know if you need further clarification. This may not be the best way to do it, but its certainly nicer then your structure. Sorry for the harsh start, but ArrayLists need to be stamped out whenever they are encountered.

Note: I shouldn’t show you this, but to solve your original problem you simply need to cast to a string. This is handy to understand, as its at the heart of OOP. But don’t fix your script, fix your structure.

textul.text = (string)arrayList_1 [1] + "ceva";

You really need to look at the error messages you’re getting.

When you do this:

textul.text = matr_1 [1, 1];//NO GO

Your console will spit out the error message “Cannot implicitly convert type int' to string’”. You’re trying to put a number somewhere you need to put a string.

You can fix it by simply turning the value into a string by concatenation:

textul.text = "" + matr_1 [1, 1];

Hope that helps! You should probably also spend a bit more time on the coding tutorials.

Looking back at my problem, the answer was that you can’t do arrays containing more than one type (float and string in my case) BUT you can do separate arrays and have them indexed in the same way so that one can easily get and store data for object 11 (just an example) from the “columns” on the “row” 11 in the 2 arrays.

For any having my problem, here is the code that does it now:

declarations:

public string[,] matr_floaturi, matr_stringuri;

in Start:

matr_floaturi = new float[300, 15];
matr_stringuri = new string[300, 6];

//        LiteraA
				matr_floaturi [10, 0] = 10f; 
				matr_floaturi [10, 1] = 0f; 
				matr_floaturi [10, 2] = 11f; 
				matr_floaturi [10, 3] = 18f; 
				matr_stringuri [10, 1] = "a"; 
				matr_stringuri [10, 2] = "LiteraA"; 
				matr_stringuri [10, 3] = "Fundal_1"; 
				matr_stringuri [10, 4] = "aaa"; 
		
				//        albină
				matr_floaturi [11, 0] = 11f; 
				matr_floaturi [11, 1] = 0f; 
				matr_floaturi [11, 2] = 0f; 
				matr_floaturi [11, 3] = 0f; 
				matr_stringuri [11, 1] = "a"; 
				matr_stringuri [11, 2] = "albină"; 
				matr_stringuri [11, 3] = "Zumzaie din floare in floare";
				matr_stringuri [11, 4] = "albină_s"; 

in Update (just excerpts but you get the ideea):

afisaj = GameObject.Find ("display");
afisaj.renderer.material.mainTexture = Resources.Load (matr_stringuri [randul, 2]) as Texture;
sunet = matr_scris [i, 0];
audio.clip = Resources.Load (sunet) as AudioClip;
audio.Play ();
matr_stringuri[Mathf.RoundToInt(matr_floaturi[j,3]),0] = matr_stringuri[i,2];

Well, this should answer my question.
PROBLEM SOLVED (…one way)