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 Daniel Greenhorn · Nov 13, 2014 at 04:59 AM · arraystringfloatread

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
     
     
     }

}

Comment
Add comment · Show 5
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 AlwaysSunny · Nov 13, 2014 at 05:27 AM 0
Share

Is there a reason you're doing this ins$$anonymous$$d of creating a class? It seems like the situation really calls for a collection of a custom class or struct whose members are ints, floats, and strings.

avatar image Daniel Greenhorn · Nov 13, 2014 at 11:56 AM 0
Share

@AlwaysSunny

I try to do a ABC app for my child. For the words database, I will need 10 photos of things for each letter (0-9 for A and so on), their positions and their pronounciations. Ideea is to randomize in each letter range) so as to keep it interesting for her. Figured an array is straightforward.

I'll try your suggestion tonight, though I wouldn't let unsolved those very basic 2D array operations. I still don't know why they don't work ( matr_1 and matr_2).

avatar image Daniel Greenhorn · Nov 15, 2014 at 03:10 AM -1
Share

The core of my problem is that there is an error somewhere in those lines where I try to define a 2D array of strings, load 2 cells and then read the values I've entered

 public string[,] matr_2;
 ...
 matr_2 = new string[10, 10];
 matr_2 [1, 1] = "Ianuary";
 matr_2 [1, 2] = "2014";
 ...
 textul.text = matr_2 [1, 1];    //the gui object
avatar image Bunny83 Daniel Greenhorn · Nov 15, 2014 at 03:23 AM 0
Share

@daniel-greenhorn:
The problem in this one is pretty obvious. You might read it again, a couple of times...

... (hint: matr_2 != matr_1)

avatar image Daniel Greenhorn · Nov 16, 2014 at 04:15 AM 0
Share

link text@bunny83 I had misspelled matr_1 ins$$anonymous$$d of matr_2. Sorry.

The problem is real, though (at least for me) in a 2D array of strings.

Please, run that code and see if you matr_2[1,1] returns something in the GUI. $$anonymous$$ine does not. The same code works with a 2D array of ints.

Better yet, I'm sending you the script. It's really untidy. I've put the problem in the first row of Update. This is really getting to me.

array script.txt (8.0 kB)

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Nov 15, 2014 at 06:53 AM

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";    
Comment
Add comment · Show 3 · 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 Daniel Greenhorn · Nov 15, 2014 at 02:29 PM 0
Share

@Bored$$anonymous$$ormon Thanks, but my problem would be instantly solved if I could create, load and read ANY 2D array (matrix).That is the core of my problem. $$anonymous$$y examples were just examples to try and get the mechanism working. Can you show me how to do a simple matrix of 2 x 2?

What I did in then end as alternative was define a ton of public const floats but the problem now is to adress them properly (same problem in other form) . Via some loops, I obtain string to match exactly the name of my const but if I use that string in Vector3 it isn't accepted (for example if i need the value stored in the OBJ20_LOCATION3_X, i can build the "OBJ"+i+"_LOCATION"+j+"_X" only it isn't accepted in vector3).

Could we discuss this offline, please? bordmail4@gmail.com. Thanks

avatar image Kiwasi · Nov 15, 2014 at 08:09 PM 1
Share

I typically answer question here because the information ends up in the public forum, and I've got plenty of utility out of other peoples answers. $$anonymous$$ind of a pay it forward thing.

I'll do private consulting in one of three cases

  1. I know the individual and have some bond of friendship or respect

  2. The project intrigues me

  3. I get paid for it

You probably have the best chance of falling into category three. Let me know if you are interested on this basis, otherwise my assistance stays here.

avatar image Kiwasi · Nov 15, 2014 at 08:13 PM 1
Share

Defining and accessing a multi dimensional array is there in my initial answer.

  • Declare the array

  • Initialise the array with a size

  • Initialise each element in the array

  • Access by index

avatar image
0

Answer by Baste · Nov 13, 2014 at 01:05 PM

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.

Comment
Add comment · Show 1 · 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 Daniel Greenhorn · Nov 13, 2014 at 04:03 PM 0
Share

@Baste certainly I should study some more as I'm bouncing off the problems. time is sparse, though. just seen that c = matr_1 [1,1] remains 0 in the inspector. I made it int, and I loaded the array with ints, like in the manual, without errors so it should match. Strange, no?

avatar image
0

Answer by Daniel Greenhorn · Dec 31, 2014 at 04:26 AM

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)

Comment
Add comment · Show 1 · 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 Eric5h5 · Dec 31, 2014 at 04:44 AM 0
Share

I'm sorry but this is incorrect. (I mean, technically it will work, but it's a bad idea and shouldn't be presented as an answer.) Bored$$anonymous$$ormon already provided the answer. Ins$$anonymous$$d of parallel arrays of different types, use a single array of a custom class, where the class contains the needed types.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array not displaying my level best time in my level select menu 0 Answers

Converting a .CSV string array to a float array 1 Answer

[SOLVED] First array slot blocking second array slot 1 Answer

C# ArrayList match to string? 1 Answer

Hierarchy index of prefab change when putting in into an array? 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