Randomly Generating combined prefabs

Hello everyone,

I am having trouble trying to figure out a way to generate a combined block from two random prefabs. There will be 8 different numbered blocks and if I had to create each combined prefab instead of randomly generating from the 8 numbered squares, I would end up with 140 combined prefabs. Before I go about this, I wanted to see if there was any C# code I could write to do this.

I have 8 numbered squares. Each prefab would display and store 1 number. Then two of the numbered blocks would spawn on screen.

Examples of how the blocks would spawn.

[1][4]
[6][3]
[2][2]
[5][4]

Hope this is enough information.

if I understand correctly, you want a model of any two prefabs to be a single object with a single mesh?

If that’s the case, you can pick those prefabs you want to be merged and create a new game object with combined meshes of those picked prefabs.

Then again, I maybe didn’t understand the question correctly, sorry if that’s the case.

If I understood you correctly, this should be pretty easy.

Step 1: Attach this script to the camera.
Step 2: Get a reference to the prefabs. You can assign them in the inspector or use ressources.load()

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

public class MyBoxBuilder : MonoBehaviour
{
    public List<GameObject> MyPrefabs;

    List<GameObject> Column1;
    List<GameObject> Column2;

    void Start()
    {
        DoLists();
        BuildBoxes();
    }

    void DoLists()
    {
        Column1 = new List<GameObject>(MyPrefabs);
        Column2 = new List<GameObject>(MyPrefabs);
    }

    void BuildBoxes()
    {


        for (int x = 0; x < 4; x++)
        {
            GameObject prefab = GetRandomBox(ref Column1);

            GameObject nextBox = Instantiate(prefab) as GameObject;
            nextBox.transform.position = new Vector3(0, x * 2, 0);

        }

        for (int x = 0; x < 4; x++)
        {
            GameObject prefab = GetRandomBox(ref Column2);

            GameObject nextBox = Instantiate(prefab) as GameObject;
            nextBox.transform.position = new Vector3(2, x * 2, 0);

        }
  
    }

    GameObject GetRandomBox(ref List<GameObject> column)
    {
        System.Random rnd = new System.Random();
        GameObject found = column[rnd.Next(column.Count)];
        column.Remove(found);
        return found;
    }

}

I hope this helps, and keep on creating!

This certainly has helped and the code worked perfectly.

I am not sure that the script should be attached to the camera if I am trying to instantiate the combined blocks as well as add other logic to the new blocks. I am going to want each new blocks to instantiate one at a time and drop to the bottom of a board similar to Tetris.

I have followed a Tetris clone tutorial and think I can handle most of the game.

Because the script does not use the gameObject it is attached to, it does not really matter where you attach it.

Any questions left? For timing (“one at a time”) I would recommend using a coroutine.