How to work with Arrays and multiple objects?

Hi,

Trying to expand what I have so it is re-usable.
Currently have 2 scripts:

<empty object 1>

Class Grid : Mono
Class Random : Mono

Grid contains: public string[,] grid = new string[51, 51];

Random references Grid: getGrid = GetComponent();

So now I want to duplicate the object, but I want each object to have a different grid size.
Since arrays are static, I can’t override each one in the inspector, so how can I go about this?

There must be a better way than creating a whole bunch of duplicate scripts just to achieve this?

Class Grid1 : public string[,] grid = new string[51, 51];
Class Grid2 : public string[,] grid = new string[71, 71];
Class Grid3 : public string[,] grid = new string[91, 91];

Class Random1: getGRID = GetComponent<Grid1>();
Class Random2: getGRID = GetComponent<Grid2>();
Class Random3: getGRID = GetComponent<Grid3>();

Thanks

Grid class.

using UnityEngine;

public class Grid : MonoBehaviour
{
    private string[,] grid;

    public void InitializeGrid(int gridX, int gridY)
    {
        grid = new string[gridX, gridY];

        Debug.Log(string.Format("grid size is: x:{0} y:{1}. On gameobject {2}",
            grid.GetLength(0), grid.GetLength(1), transform.name));
    }
}

Grid maker class

using UnityEngine;

public class GridMaker : MonoBehaviour
{
    public void MakeNewGrid()
    {
        Grid newGrid = gameObject.AddComponent<Grid>() as Grid;

        int randomX = Random.Range(0, 100);
        int randomY = Random.Range(0, 100);

        newGrid.InitializeGrid(randomX, randomY);
    }

    public void MakeNewGridAndObject()
    {
        GameObject go = new GameObject("new object");
        Grid newGrid = go.AddComponent<Grid>() as Grid;

        int randomX = Random.Range(0, 100);
        int randomY = Random.Range(0, 100);

        newGrid.InitializeGrid(randomX, randomY);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.N))
        {
            MakeNewGrid();
        }

        if (Input.GetKeyDown(KeyCode.M))
        {
            MakeNewGridAndObject();
        }
    }
}

To use, put GridMaker on any gameobject
Press N to put a random grid on the same gameobject
Press M to put a random grid on a new gameobject

Should get you started if understand correctly this time :sweat_smile:

If you don’t care about having a separate script make new grids, just generate some random numbers when your grid class starts.

using UnityEngine;

public class Grid : MonoBehaviour
{
    public string[,] grid;

    private void Start()
    {
        grid = new string[Random.Range(0, 100), Random.Range(0, 100)];

        Debug.Log(string.Format("grid size is: x:{0} y:{1}. On gameobject {2}",
        grid.GetLength(0), grid.GetLength(1), transform.name));
    }
}

Wow thanks for that…
I’m going to have to play around with it for a bit to get my head around it.

And heres the original one I posted and deleted. I missunderstood the question. The above is random as requested, the below is if you wanted to define the grid sizes in advance. Sorry about that.

How about using scriptable objects?

Something like:

using UnityEngine;

[CreateAssetMenu(fileName = "GridDefinition", menuName = "ScriptableObjects/GridDefinition", order = 0)]
public class GridDefinition : ScriptableObject
{
    public int gridX;
    public int gridY;
}

and your grid class

using UnityEngine;

public class Grid : MonoBehaviour
{
    public GridDefinition gridDefition;

    public string[,] grid;

    void Start()
    {
        grid = new string[gridDefition.gridX, gridDefition.gridY];
    }
}

Put your Grid class on a gameobject
Use Asets → Create → ScriptableObjects → GridDefinition to make a new definition
Define your X and Y in the new scriptable object
Drag/Drop the scriptable object on to the public GridDefinition field in the Grid class
Repeat for as many grid sizes as you want to define

Alternatively you could read the data in from an XML, JSON or other file if you wanted.

You could load up a dictionary with key value pairs as a <id, GridSize> and choose which you want at runtime based on some input.

Lots of different approaches to this :slight_smile: