- Home /
Can you help me with arrays?
I know there is a solution to this. It seems that I used to know how to do this. Is there a way to make, say, an array, and in this array, have objects with "options" or more arrays?
Answer by ankush_Kushwaha · Feb 13, 2014 at 09:21 AM
You can use the class say for user (which has a name and a lot of friends). And we are going to add those friends name into array.
class user
{
public string Name { get; set; }
public List<user> friendList { get; set; }
}
Now make an object
user u = new user();
u.Name = name.str;u. = name.str;
u.friendList = new List<user>();
Now you can add friends of type user into the u's friendList.
user friend = new user();
friend.Name = "john";
u.friendList.Add (friend);
Add as many friends in u's friend list array as you want. And You can add friend of friend's (array into array) and their Name
(option).
I guess This is exactly, what you wanted :)
Answer by ProtoTerminator · Feb 13, 2014 at 01:38 AM
Not sure if it's what you mean, but you can do 2-dimensional arrays:
var twoDArray: Transform[,] = new Transform[width,length];
I think this is what I was looking for. I will make this the answer if it works.
I am looking to embed elements in elements. I don't know if that is what a 2-dimensional array does or not, though. This is an example of what it would look like in the inspector (not code):
V Element 0
Sun [transform]
V Element 0
Color [Color]
Intensity [Float]
> Element 1
var myArrayList: ArrayList = new ArrayList();
var myArray1: ArrayList = new ArrayList();
var myArray2: ArrayList = new ArrayList();
function Start(){
myArray1.add(1);
myArray1.add("something");
myArray2.add(true);
myArray2.add("transform");
myArrayList.add(myArray1);
myArrayList.add(myArray2);
}
This is basically setting up your own 2D array that can accept any type of data, which is looks like is what you want. $$anonymous$$eep in $$anonymous$$d that it's not going to show in the inspector just how you want. I'm actually not exactly sure what the myArrayList will looks like when you add the other arrays, but you'll have to see for yourself. And you won't be able to add anything through the inspector, you'll have to do it all through code.
This is how you access the elements:
for (i in myArrayList){
for (j in i){
if (j){
//do something
}
}
}
It will be a pain, but here you go. I hope this helps.
By the way, I'm sure there are simpler ways to do what you want, I just have no idea what it is you want to do.