- Home /
Strange behaviour after creating array of Blocks
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];
}
}
}
Ins$$anonymous$$d of just dumping 100 lines of code and waiting for somebody to read, understand, and reproduce in his heads what happens by pressing Play, how about you telling us what it's supposed to do, what do you expect to happen, and what do you actually want to happen ins$$anonymous$$d?
Explanations such as "$$anonymous$$ problem", "everything goes wrong", and "accours a problem" are not exactly helpful when describing an error...
Sorry, but I don't know how explain well, so I posted the code, took one picture and explain some.
Here what happened when I press play, and what is happening:
When I hit space bar, the white cube destroy what is under it,but for a unknown reason the 8,8 block goes up. This happening when I changed the array of gameObjects for a array of type block.
When the block destroy something else, all the blocks going up and are destroyed when they reached the 9 line of blocks.
The first line of blocks are created when the 9 are destroyed.
Answer by Wolfram · Jan 31, 2013 at 10:56 PM
You only have a single object "block" in your whole manager. You initialize that in Awake(), but after that you just keep reassigning to block.obj of that one object. Note that CreateCube() doesn't actually create an object of type Block, it just returns a reference to your one BlockManager.block.
In the end, your whole array is filled with references to that same object, so there also is only one value block.obj, and it points to the last object you created in the last call to CreateCube, i.e. the object for CreateCube(7,7).
Solution: Move this line:
block = new Block();
from Awake() to the beginning of CreateCube(). Also move the member variable block
(line 20) into CreateCube as well.
Strange the first time when I was using a array of gameobjects, I had done this way, but was creating besides the cubes some empty game objects too.
But now I remember, I had created a variable outside the method CreateCube, and only was initialized inside the method.
Thaks you for the help but now strange things keeps happening.
When I hit spacebar to destroy a cube, they are destroy with some lag, and the variables positionX,positionY are accessed a lot of times ![alt text][1] [1]: /storage/temp/7392-untitled-2.jpg
When I hit twice or more, the wall of cubes doesn't move anymore. I don't know what's happening <_<
That's impossible to tell without seeing the code you execute when pressing Space (including any functions that are called).
Answer by hherzog · Feb 01, 2013 at 08:59 PM
Here the code:
public class Esfarelante : MonoBehaviour {
bool stepInsomething = false;
Collision collision;
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(new Vector3(-1, 0, 0) *
Time.deltaTime * 3.5f);
}
if (Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector3(1, 0, 0) *
Time.deltaTime * 3.5f);
}
if (Input.GetKeyUp((KeyCode.LeftControl)))
{
if (stepInsomething)
{
BlocksManager.instance.ChangeState(this.collision);
}
}
}
void OnCollisionStay(Collision collision)
{
stepInsomething = true;
this.collision = collision;
}
void OnCollisionExit(Collision collision)
{
stepInsomething = false;
}
}
This is not the script that handles pressing the spacebar.
Also, please post this new problem as a new question, as it is relatively unrelated to your original question, which has been answered and solved.
When I hit spacebar to destroy a cube, they are destroy with some lag, and the variables positionX,positionY are accessed a lot of times
$$anonymous$$oveUpwards() contains a nested 8x8 loop, with 128 print statements in total. This is what makes it slow. Comment out both print statements, and it will run much faster.
Too many possible factors, too many possibilities. Also, we don't know how you did fix the reference problem, which could have other side effects.
If the GameOBject approach worked for you, I'd suggest you use it, and ins$$anonymous$$d of having the standalone Block-classes referencing your object, have Block derive from $$anonymous$$onoBehaviour, and add it as a regular component to your GameObject, as soon as you instanced that (=directly after your GameObject.CreatePrimitive
line).
Your answer
![](https://koobas.hobune.stream/wayback/20220613091845im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I see a sky through the building 1 Answer
Instantiate from array into array? 2 Answers
how to check for GameObject is null in array with random 1 Answer
Use an objects (from array) position to focus a camera on 3 Answers
Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers