Multidimensional array?

hi everyone, i need some help
i have two problems
the first one is, how can i create a nested array like the image i uploaded? (i used paint… )
and the second is, how can i access it from another script?
to makes things more clear, this is what i’m trying to get
in a empty gameobject there will be the array manager script(with all the arrays). in another script i call one of those array (blue, red, yellow) and with a bool system i access one or more of the gameobjects in the secondary arrays. so if i’m in blue array and bool x = true, random select between flat-big-little, if y= true select randomly between big and-little and so on and then instantiate that gameobject (choosed again randomly)

My suggestion would be to create a class that contains the variables for the GameObjects inside of it. This class can represent your bold, flat, big, little.
Next create another class that acts as a container for the before mentioned class, inside it would be a length 4 array that holds the bold, flat, big, little. Inside this class include a public function that takes a bool that implements the logic of randomly selecting the data you need from the array and returns.
Next create another class, this will be your array manager script. This script will hold the blue, red, and yellow and you can include a function to get each of these for any class that requests it outside of the ArrayManager for instantiation.

It is best not to create nested array because the logic becomes exponentially more difficult to keep track of. The reason for creating the three class is because any arrays will stay as one dimension and this also makes this more modular and easier to maintain. With these classes you will be able to add and delete things with less risk of messing something up somewhere else

Here is a skeleton you can play around with:

public class SomeClass {
    private GameObject[] objects;

    public SomeClass() {}
    public GetGameObject(int id) {}
}

public class SomeClassContainer {
    private SomeClass[] types; //bold, flat, big, little

    public SomeClassContainer() {}
    public SomeClass PickRandomType(bool someValue) {}
}

public class ArrayManager : MonoBehaviour {
    private SomeClassContainer blue;
    private SomeClassContainer red;
    private SomeClassContainer yellow;

    public SomeClassContainer GetBlue() {}
    public SomeClassContainer GetRed() {}
    public SomeClassContainer GetYellow() {}
}
1 Like

I’d like to say that I have understood. I never used classes (my fault) and that’s why i thought a multidimensional array. could you explain that skeleton a little bit more?
another thing, every gameobject has a script (the same) where you can check some bools. in another script i check the true bools of the gameobject and i decide what i can instantiate next (if bold, flat and so on)… so the bool system is in another script

So essentially what I’m suggesting that you do is split up your idea into their own separate classes that way they fit together without the unnecessary logic.
Lets look at your picture:

Look at what everything has in common. Blue, Red and Yellow all contain the same thing: bold, flat, big, little. This means that can all be combined and represented as a single class instead a row in an array. Combining these into a class also allows you to implement logic that is specific to how you would like Blue, Red and Yellow to handle its contents(bold, flat, big, little) with whatever given boolean value.

Next if look at the four types you have list, you can notice they each have the same thing in common as well. They all contain 1 or 2 (maybe more) GameObjects. That means we can create a class to represent these so that the logic for these is also handled the same across the board.

A good thing about uses class is that when you change one thing in the class everything that uses that class will change with it so logic is preserved for any object that uses this class. You’ll also notice if you start dividing stuff up into class that whatever scripts you are using to handle the classes will shrink in the amount of code needed to handle and keep track of everything.

With a multidimensional array to handle something like this would be tricky and difficult because of all the different variables you will need to keep track of, and this means the amount of code needed would increase drastically.

The skeleton I gave you is an example of how to split the above picture into its common components so that it is easier to manage, maintain and implement. I would highly recommend you do some research and learn more about classes and struct as they are very important data structures. I hope that explained it well enough, if not I can go into more detail.

If the bool system is in another script, that is alright. With the above skeleton you can do something like the following:

ArrayManager am = WhateverGameObjectThatHasThisScript.GetComponent<ArrayManager>();
SomeClass randomObject = am.GetBlue().PickRandomType(someBool)

The above code snippet is a scenario where you want to get a random object (bold, flat, big, little) from the blue category based on the variable in your bool system script.

1 Like

that was perfectly clear, great explenation! do you know some good tutorials about classes? they are extremely useful :o
but i’ve got other questions. how PickRandomType(bool someValue) works? it’s just like a function?
With getblue i get in blue class but then how pickrandomtype acces to bold ecc? what i want to say is how can i say to the system: ‘hey, x = true and y= true, choose one element from bold or flat’
sorry for all this dumb questions, my mind is a bit messy now

Googling C# Classes and the first two links should help clear stuff up. Also, the Unity Classes tutorial is a great resource.
https://unity3d.com/learn/tutorials/modules/beginner/scripting/classes

The function works as any other function would work. Take in the booleans and use if statements to determine how to handle the combinations of booleans sent in. Lets say for example that you are picking the types based off the combinations of booleans X and Y. Your logic could look something like this:

public SomeClass PickRandomType(bool x, bool y){
    if (x == false && y == false) {
      return types[1].GetRandomGameObject(); // Rnadom object from bold
    } else if (x == true && y == false) {
      return types[2].GetRandomGameObject(); // Random object from flat
    } else if (x == false && y == true) {
      return types[3].GetRandomGameObject(); // Random object from big
    } else if (x == true && y == true) {
      return types[4].GetRandomGameObject();  // Random object from little
    }
    return null;
}

And in the SomeClass class the GetRandomObject would look something like:

public GameObject GetRandomGameObject() {
    return objects[Random.Range(0, objects.Count - 1)]; // return a random object in the array of GameObjects
}

Now this code may vary from however you are looking for the logic to work, but you should get the general idea.

1 Like

i’ve understood all i have to do now to get it down, but i have three more dumb questions. first, there’s a way to return more types and randomly select one of them and then, after one is picked use getrandomgameobject? i mean if x == true, y== false, random choice between bold and flat (because i could use both), then flat(or bold).getrandomgameobject();
2nd why i keep getting this error:
error CS1520: Class, struct, or interface method must have a return type (in public GetGameObject(int id) {})
and 3d rror CS1519: Unexpected symbol `(’ in class, struct, or interface member declaration (in public class SomeClassContainer() {)
thanks for your support… :sweat_smile:

To randomly select between multiple types, you will need to implement that within the PickType function().

Your first error is because you do not have a return type in the function header.

public GetGameObject(int id) { }
public GameObject GetGameObject(int id) { }

The second error is because you have a parenthesis at the end instead of a curly bracket.

public class SomeClassContainer() { )
public class SomeClassContainer() { }
1 Like

ok :slight_smile:
in the second error the curly bracket is there because i wrote " ( in public class SomeClassContainer() { ) " in the post, but there isn’t one in the code so i still have the error

My bad I read that wrong. I had a typo in the skeleton, just remove the parenthesis.

it’s time i study classes, other problem pop up, error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to Array.SomeClass’ in pickrandomtype function

Change the data type from SomeClass to GameObject for the PickRandomType function.

how about posting the code you’re now referring to?

You have three functions in Array class that do not return any values even though they should, their implementation is empty.

public SomeClassContainer GetBlue() {return null;}
public SomeClassContainer GetRed() {return null;}
public SomeClassContainer GetYellow() {return null;}

If you make them return anything, like null, your code will compile. However the fact that you did not see this, says that you do not understand, very basic things about coding and probably should start with something simpler.