Loading main class

Hi,

I’ve loaded a set of cubes through a loop in the “Start()” function. To initialize this script, I’ve placed it into an empty object.

Now I notice that the position of the cubes is random, they shift Y positions every time you run it, but the “wall” of cubes is well aligned. This makes me think I’m depending on the framerate somehow ?

How do you initialize the main class, where I store my game-loop and initialize the level ?
Is it correct to do this in an empty object and placing this object in the stage ?

Your approach seems to be correct, there is no "main class " in unity, you could make a class that manages initialization like you did, and attach it to an object.

Can you try to post the code in the Start() function? The overall approach seems to be correct, so the problem must be in the Start code itself.

My test was is made from parts of other examples. Sometimes the first cube is placed on y:-20, sometimes its on y:-60, I don’t see why it’s randomized like this. The X and Y are relatively correct but the starting point is wrong so it’s never on x:0, Y:0.

void Start() {
		for (int y = 0; y < 5; y++) {
			for (int x = 0; x < 5; x++) {
				GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
				cube.AddComponent<Rigidbody>();
				cube.transform.position = new Vector3(x, y, 0);
				Texture2D p = new Texture2D(100, 100);
				p = Resources.Load("Floor 64x64", typeof(Texture2D)) as Texture2D;
				cube.renderer.material.mainTexture = p;	
			}
		}
	}

I’ve just tried this code on an empty scene and it works. (Note that there is a rigidbody attached so the cubes start falling immediately, changing the y value). Try to remove the rigidbody part, just to check if the spawn position is correct. If this work we can solve this problem later. :slight_smile: