GameObject variable error please help :)

So, i was following a tutorial about 2D producerally generated mesh, but there’s an error that’s not giving me a posible solution so i come here.
Here’s my Script:

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

public class GenerateChunk : MonoBehaviour {

public GameObject DirtTile;
public GameObject GrassTile;
public GameObject StoneTile;

public int width;
public float heightMultiplier;

// Use this for initialization
void Start () {
    Generate ();
}

// Update is called once per frame
public void Generate () {
	for (int i = 0; i < width; i++)
    {
        int h = Mathf.RoundToInt(Mathf.PerlinNoise(0f, i/3));
        
        for (int j = 0; j < h; j++)   
        {
            GameObject selectedTile;
            if (j < h - 4)
            {
                selectedTile = StoneTile;
            }
            Instantiate(**selectedTile**, new Vector3(i, j), Quaternion.identity);
        }
        
    }
        
}

}

The bold phrase is giving the error, it says “Assets/Scripts/GenerateChunk.cs(32,29): error CS0165: Use of unassigned local variable `selectedTile’” I tried everything, but it’s not changing. Please help and thank you in advance.

If your if condition “(j < h - 4)” is false, then selectedTile will have never been assigned to, but you will try to instantiate it anyway. This won’t end well, and the compiler is telling you that.

I don’t know the original tutorial, but this can be solved by putting your Instantiate call in the if condition, so you only use selectedTile if you have actually assigned something to it.

             GameObject selectedTile;
             if (j < h - 4)
             {
                 selectedTile = StoneTile;
                 Instantiate(selectedTile, new Vector3(i, j), Quaternion.identity);
             }

Now, you will only use the variable selectedTile if it has actually been assigned to.

I tried everything

For future questions, it’s worth including what you have tried, so the people helping you won’t have to spend their time trying something you already know doesn’t work.

i saw your problem.

The “instantiate” method don’t see this instantiated:

GameObject selectedTile;

But why?!?!?!

Because everything that you create(not in this case, but is good know it) in Curly brackets { } will be avaliable only inside in that curly brackets.
But the main problem, is in your if statement.

Remember: “If this condition is true, whatever inside curly brackets will be execute”.

But what happen is not?

selectedTile never will get an instance, and worse(Is you put null the selectedTile and not check it), will give you a NullReferenceException the Instantiate Method.

Okay… now, how fix this?

public void Generate () {
 for (int i = 0; i < width; i++)
 {
     int h = Mathf.RoundToInt(Mathf.PerlinNoise(0f, i/3));
     
     for (int j = 0; j < h; j++)   
     {
         GameObject selectedTile = null;
         if (j < h - 4)
         {
             selectedTile = StoneTile;
         }
         if(selectedTile != null)
         Instantiate(selectedTile, new Vector3(i, j), Quaternion.identity);
     }
     
 }

Set the “selectedTile” to null BUT check in your “instantiate” method is your selectedTile GameObject is null or not.