I am trying to Instantiate() different prefabs in a runtime, however when trying to do so, I get error UnassignedReferenceException: The variable prefab of Environment has not been assigned, although one of the prefabs is being instantiating successfully.
I’ve double checked that all the prefabs are assigned to the script in the inspector, and after that I assigned the same prefab (the only working one) to all the variables. Now it looks like this
When running this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Environment : MonoBehaviour
{
public GameObject roadPrefab;
public GameObject crossRoadPrefab;
public GameObject emptyTilePrefab;
public GameObject prefab;
public int horizontalRoads = 10;
public int verticalRoads = 10;
public int mapScale = 100;
public int roadWidth = 4;
// Start is called before the first frame update
void Start()
{
//GenerateGrid();
// roadPrefab
if (roadPrefab)
{
Instantiate(roadPrefab);
}
else
{
Debug.Log("No roafPrefab");
}
// crossRoadPrefab
if (crossRoadPrefab)
{
Instantiate(crossRoadPrefab);
}
else
{
Debug.Log("No crossRoadPrefab");
}
// emptyTilePrefab
if (emptyTilePrefab)
{
Instantiate(emptyTilePrefab);
}
else
{
Debug.Log("No emptyTilePrefab");
}
// prefab
if (prefab)
{
Instantiate(prefab);
}
else
{
Debug.Log("No prefab");
}
}
The output is following: roadPrefab, crossRoadPrefab and prefab are failing, while emptyTilePrefab is getting instantiated successfully.
Does anyone has any ideas on why this happening?