Trying to generate 2d background blocks behind foreground ones

So I am trying to make a simple 2D sand box game, and I’m trying to generate the background tiles behind the foreground ones, but when I generate my world the background tiles are no where to be seen.
Here’s my code to Generate a chunk:

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

public class WorldGeneration : MonoBehaviour {

public GameObject dirtTile;
public GameObject grassTile;
public GameObject stoneTile;
public GameObject dirtBackground;
public GameObject stoneBackground;

public int width;
public float heightMultiplier;
public int heightAddition;

public float smoothness;

[HideInInspector]
public float seed;

void Start ()
{
    Generate();
    seed = Random.Range(-10000f, 10000f);

}

public void Generate()
{
    for (int i = 0; i < width; i++)
    {
        int h = Mathf.RoundToInt(Mathf.PerlinNoise(seed, (i + transform.position.x) / smoothness) * heightMultiplier) + heightAddition;
        for (int j = 0; j < h; j++)
        {
            GameObject selectedTile;
            GameObject backgroundTile;
            if(j < h - 4)
            {
                backgroundTile = stoneBackground;
                selectedTile = stoneTile;

                
                
            } else if (j < h - 1)
            {
                selectedTile = dirtTile;
                backgroundTile = dirtBackground;

            }
            else
            {
                backgroundTile = dirtBackground;
                selectedTile = grassTile;
            }
            GameObject newTile = Instantiate(selectedTile, Vector3.zero, Quaternion.identity) as GameObject;

            GameObject newBackgroundTile = Instantiate(backgroundTile, Vector3.zero, Quaternion.identity) as GameObject;

            newTile.transform.parent = this.gameObject.transform;
            newTile.transform.localPosition = new Vector3(i, j);
        }
    }

}

}

Hi @CaptainPickle77,

Assuming you are using prefabs to fill the “Tile” and “Background” GameObject variables of your script, you can take a look at SpriteRenderer component of the prafabs and check the values of the “Sorting Layer” and “Order in Layer” fields. If “Tile” and “Background” prefabs are in the same Sorting Layer but the “Tile” have a Order in Layer value smaller than the “Background”, the tiles will always stay behind the background. If they have the same Order in Layer value you still can have unwanted behaviours, its is best to set different Order in Layer values for tiles and background, maybe event different Sorting Layers.

If the situation I described is really your problem, you can also check this Unity official tutorial about the subject: Unity for 2D: New Workflows in Unity 4.3 - Unity Learn

That doesn’t see to be the problem it is throwing this exception:

Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transform:set_parent(Transform)
WorldGeneration:Generate() (at Assets/Scripts/WorldGeneration.cs:64)
WorldGeneration:Start() (at Assets/Scripts/WorldGeneration.cs:24)