Hello everyone, I’m trying to spawn my game objects according to this JSON. Unfortunately, I couldn’t make this happen, I’m stucked. Can someone help me to achieve this?
{ “_matrix”: [
1, 0, 1, 2, 1,
0, 1, 0, 1, 2,
2, 2, 1, 2, 1,
1, 1, 2, 1, 0,
0, 2, 0, 0, 1,
2, 0, 2, 0, 1 ]}
- at 0 there will be cherry,
- at 1 there will be banana,
- and at 2 there will be watermelon.
This is my JSONReader Class:
[Serializable]
public class JSONReader
{
public List<int> _matrix;
private void Awake()
{
_matrix = new List<int>();
LoadItem("Assets/Resources/_matrix.json");
}
public void LoadItem(string path)
{
string myText = File.ReadAllText(path);
_matrix = JsonUtility.FromJson<List<int>>(myText);
}
and This is my Grid Generator Class;
Using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;
public class GridGenerator : MonoBehaviour
{
public int height = 6;
public int width = 5;
private float GridSpaceSize = 1.5f;
public List<GameObject> foods;
private GameObject[,] _gameGrid;
private void Awake()
{
CreateGrid();
Debug.Log(FillTheMatrix.LoadItem());
}
private void CreateGrid()
{
_gameGrid = new GameObject[width, height];
List<int> _matrix = FillTheMatrix.LoadItem();
if (foods == null)
{
Debug.LogError("ERROR: Grid Cell Prefab on the Game grid is not assigned");
}
// TO DO 3 MATCH GAME
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
_gameGrid[x, y] = Instantiate(foods[Random.Range(0,3)], new Vector3(x * GridSpaceSize - 3, 0.5f, y * GridSpaceSize - 3),
Quaternion.identity);
_gameGrid[x, y].transform.parent = transform;
_gameGrid[x,y].gameObject.name = "Grid Space ( X: " + x.ToString() + " , Y: " + y.ToString() + ")";
}
}
}
}