- Home /
Best way to match up 3 bits of data
Hi Everyone, Sorry about the vague title. I couldn't think of anything better.
At the moment I'm creating a level selection menu, currently I'm storing the level information in arrays:
var levelsList : String[];
var levelsPictureList : Texture2D[];
var levelsDiscriptionList : String[];
Now the problem with this is we have to manually check that the array indexes match up(so index 0 in each array corresponds to the data for level 1 etc.). I can already tell that this is going to cause issues for us later on.
My question really is; How would I store 3 data types in the same Object, and be able to easily access each bit of data seperately?
I've been looking into multidimensional arrays, which is not something I've used before. Is this the way forward? If so, any pointers on how to best use a 3d array?
I was also thinking about Serialization. Again, not something I've used before, would this be appropriate for what I'm trying to achieve? I'm totally new to serialization, so if this is the best option I might need some guidance :(.
I'm currently at work but you definitly want to learn about classes. Look on the forums for the Newbie Guide to Unity Javascript, you could learn something useful :)
Answer by Eric5h5 · Feb 21, 2012 at 09:23 PM
You should make a class:
class LevelData {
var name : String;
var picture : Texture2D;
var description : String;
function LevelData (name : String, picture : Texture2D, description : String) {
this.name = name;
this.picture = picture;
this.description = description;
}
}
Then:
var levelData = new LevelData[10];
levelData[0] = new LevelData ("Level1", level1pic, "The first level.");
For more information see JScript.NET classes. You can also look up C# classes since it's mostly the same.
Edit: Ok, read up a bit. Looks like this is a construct, right? I'm guessing you can't edit this sort of thing in the editor by default?
A construct? No, it's a class. And yes you can edit it in the editor. You don't have to do anything, it just works by default (unlike C# ;) ... response to comments on the other answer).
By the way, I tried to edit the answer to correct the code (should be "var name : String, var picture : Texture2D, etc.). But it seems editing doesn't work anymore.
give the edit a couple of hours to show up, last time i edited something it needed that long to show up as well -.-
that is kinda weird (the working by default in unityscript and not in c#) because as i understand it, it shouldn't matter at all what language you use, right?
ah ok, so it isn't really a problem after all. Just add the attribute and you are good to go. thx for the link
Answer by ZweiD · Feb 21, 2012 at 06:04 PM
do it like this:
var levels : Object[];
and store the level-Object like this:
levels.push({
name: 'level1',
picture: new Texture2D(),
description: 'the first level'
});
Then you can access the information like this:
levels[0].name
// or
levels[0]['name']
disclaimer: don't know if that works in unityscript, only know it works in javascript
UnityScript is JavaScript. Or, to be more accurate, a modified version of traditional JavaScript.
Not really; Unityscript isn't much like Javascript at all. It's more like ActionScript3 or JScript.NET
. For example, the code in this answer won't work at all I'm afraid.
ok, but why the down vote?
the basic principle still applies and I said that I am not sure the code does work.
what you are doing is basically the same thing that I do, just a bit more verbose.
Thanks for the answer ZwieD. I had no idea you could declare type "Object" though, so it wasn't all a waste :).
The argument of what UnityScript is exactly seems as old as Unity itself. I remember reading an official description from UT somewhere, might be worth looking for it.
You are welcome.
that's why I'm using C# and not unityscript, as it seems kinda unclear what you can and what you cannot do (compared to javascript).
I don't really see a point in using unityscript if you loose all of the flexibility of javascript (like in my example: providing an object on the go without first declaring a class / struct) and have to use a more verbose syntax.
But that may only be me because I like 'wrestling' with javascript sometimes ;)
Your answer

Follow this Question
Related Questions
Serialize custom multidimensional array from Inspector 3 Answers
How to make custom 2 dimensional enum array class 1 Answer
How do you work with multi-dimentional arrays 2 Answers
Save/load playerprefs 2 Answers
Saving/Load using menu C# 1 Answer