Get() and Set() values of a gridArray from another scipt
Good day, I am very new to Unity, and I'm making an application that simulates a breadboard based on collisions. I have a GridMap script connected to my Grid. It contains a two-dimensional gridArray on it, as well as Getters and Setters. I looks something like this:
public class GridMap : MonoBehaviour
{
public int width;
public int height;
public int[,] gridArray;
}
public void Start()
{
Init(20, 63); //The size of my grid
}
public void Init(int width, int height)
{
this.width = width;
this.height = height;
gridArray = new int[width, height];
for (int x = 0; x < gridArray.GetLength(0); x++)
{
for (int z = 0; z < gridArray.GetLength(1); z++)
{
//DoSomething
}
}
}
public int Get(int x, int z)
{
if (CheckPosition(x, z) == 0) //outofbounds error detection
{
return 0;
}
return gridArray[x, z];
}
public void Set(int x, int z, int value)
{
if(CheckPosition(x,z) == 0) //outofbounds error detection
{
return;
}
gridArray[x, z] = value;
}
I also have a script on another object called BreadboardLogic which acts as a listener for Collisions, then detects what cellPositions those Collisions occured at. Its main method looks like this:
private void OnCollisionEnter(Collision collision)
{
Collider collider = collision.GetContact(0).thisCollider;
collisionPoint = collision.GetContact(0).point;
cellPosition = grid.WorldToCell(collisionPoint);
Debug.Log(cellPosition);
Debug.Log(collision.GetContact(0).otherCollider);
}
With this, I am able to detect the Vector3Int cellPositions of the collisions and print it on Debug, however I would like use them to Set() the value of those respective grid positions to 1 on my gridArray. What would be the best way to do this? I first assumed it was something like this:
gridMap.Set(cellPosition.x, cellPosition.y, 1);
But it doesn't work T_T Any ideas would be appreciated, thanks in advance!