Autobuilding blocks (The lazy mans answer to block placment)

Hi all,

I just thought I would pop in and say hi, just finished the Block Breaker section of the course and have been busy working through my version of the game.

Daunting!! There are so many great efforts here, I can see I am in the right place for feed back. But not yet, I haven’t ironed out all the creases, I am still awaiting the arrival of a road roller from Ebay for that…

However I have come bearing gifts which I would love to get feed back on and maybe open a discussion which would lead to a greater final script version…

Myself, well I am on the 30% mark of this unity course, I have completed two others. Both, like this one, extremely good and at present I am happily learning* c# for beginners with Mosh Hamedani and a great free course if you haven’t seen it called 2d Game art for non artists. Other than that I am a total beginner, what I lack in skill and ability I make up for in persistence and sometimes brute force…

Anyway I would appreciate you thoughts, discussion, ideas and criticism on my little side project of laying out blocks as level starts to save my sanity. The Gif shows the same evel 3 or 4 times so you get a feel for what the script is doing.

And the code… This script is attached to LevelManager and runs at start…

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AutoBuild : MonoBehaviour {

public GameObject[] PrefabBlocks;
private int yValue;
private int xValue;
private int[] yPosition = new int[4];
void Start()
{
    RandomYValues();
}
void RandomYValues()
{
    List<int> RandomY = new List<int>();
    for (int i = 0; i <= yPosition.Length -1; i++)
    {
        int value = Random.Range(5, 11);
        if (!RandomY.Contains(value))
        {
            RandomY.Add(value);
        }
        else i -= 1;
    }
    for (int v = 0; v < yPosition.Length; v++)
    {
        yPosition[v] = RandomY[v];
        Debug.Log("I am now an array " + yPosition[v]);
    }
    RandomY.Clear();
    StartingPositions();
}

void StartingPositions()
{
    xValue = 2;
    for (int  i = 0;  i <= yPosition.Length -1 ;  i++)
    {
        yValue = yPosition[i];
        RowBuild(yValue);
    }
}
void RowBuild(int yPos)
{
    for (int i = 0; i <= 11; i++)
    {
        int blockIndex = Random.Range(0, 4);
        GameObject PrefabColour = PrefabBlocks[blockIndex];
        Vector3 position = new Vector3((xValue + i), yPos, 0.0f);
        Instantiate(PrefabColour, position, Quaternion.identity);
    } 
}

}

Sorry if the format is wrong I will correct it on contact…

Anyway I would love to get some feedback and ideas…

I want to eventually create patterns, like the wonderful ones people have done on here. Also would like to add indestructible game objects ect. But I am happy for now with this. As a side note I did write this originally without a list choice for random numbers. I instantiated the blocks with a variable and used var.transfrom.position.y. Then used a large Vector 3 array of [48] to store all the positions to compare the y values to stop overwriting each other. It became, large, ugly and unwieldy for my low skill level. I think I may have to go back there for task of adding indestructible’s? Thoughts on that?

  • definition of happy may differ from others, screaming and hair pulling often involved

Privacy & Terms