I’m trying to create a wall of cubes and then shift their reference in the array and update their position,
but when I tried to create a class to the cubes and inside of it a reference to a gameobject , everything goes wrong
public class Block
{
public GameObject obj;
public Block()
{
}
}
Public class BlocksManager : MonoBehaviour
{
//global var
Block[,] array;
Object[] textures;
// Reference to another scrips
public static BlocksManager instance;
//the block it self
Block block;
void Awake()
{
instance = this;
block = new Block();
}
void Start()
{
// reference to the cubes
//left space to create more after
array = new Block[9, 9];
//Load imagens of the especific patch
textures = Resources.LoadAll("Textures");
//create 8X8 Blocks
for (int i = 0; i <= 7; i++)
{
for (int j = 0; j <= 7; j++)
{
array[i, j] = CreateCube(i, j);
array[i, j].obj.name = "" + i + "," + j;
}
}
Block CreateCube(int i, int j)
{
float referenceX = -2.071273f;
float referenceY = -2.273989f;
float cubeWidth = 0.60f;
float cubeHeight = 0.60f;
int i_ = i;
int j_ = j;
block.obj = GameObject.CreatePrimitive(PrimitiveType.Cube);
block.obj.transform.position = new Vector3(referenceX + j_ * cubeWidth, referenceY + i_ * cubeHeight, 0);
block.obj.transform.localScale = new Vector3(0.60f, 0.60f, 0.60f);
block.obj.renderer.material = new Material(Shader.Find("Transparent/Diffuse"));
block.obj.renderer.material.mainTexture = textures[0] as Texture;
return block;
}
public void ChangeState(Collision colission)
{
//get the reference of the object
//string[] array = colission.gameObject.name.Split(',');
colission.gameObject.renderer.enabled = false;
colission.gameObject.collider.enabled = false;
MoveUpwards();
}
public void ChangeState(Collision colission)
{
colission.gameObject.renderer.enabled = false;
colission.gameObject.collider.enabled = false;
MoveUpwards();
}
Here’s the problem, when this is executed accours a problem with the blocks, when I used a array of GameObjects instead a array of Block everything works fine
public void MoveUpwards()
{
for (int i = 7; i >= 0; i--)
{
for (int j = 7; j >= 0; j--)
{
//move the blocks upwards
float positionX = array[i, j].obj.transform.position.x;
float positionY = array[i, j].obj.transform.position.y;
print("I" + positionX);
print("J" + positionY);
array[i, j].obj.transform.position = new Vector2(positionX, positionY + 0.99f * 0.60f);
//shift the reference
array[i + 1, j] = array[i, j];
}
}
}