I am creating a mapping utility for creating open world maps. Right now I am just using empty sprites with colors.
public class Map : MonoBehaviour
{
public List<Room> rooms;
...
[ContextMenu("AddRoom")]
private void AddRoom()
{
Object roomLoadedPrefab = Resources.Load<GameObject>(paths["MapUnit"]);
GameObject room = (GameObject) PrefabUtility.InstantiatePrefab(roomLoadedPrefab);
for(int i = 0; i < rooms.Count; i++)
{
Debug.Log($"roomPrefab: {room}");
if(rooms[i] == null)
/* If there is an empty element available, put this room in that element */
{
rooms[i] = room.GetComponent<Room>();
}
else if(i <= rooms.Count-1)
/* If this is the last element and is filled, or out of bounds, add this room to the end of the List<Room> */
{
rooms.Add(room.GetComponent<Room>());
}
}
}
}
I am trying to look for empty elements in my List<Room> to fill, or add it to the end of the list.
According to the documentation, my prefab ought to be created in the scene and returned by PrefabUtility.InstantiatePrefab()
I then want to take the reference the Room component inside my List<Room>.
When I Debug.Log each element, I get the following error:
roomPrefab:
UnityEngine.Debug:Log (object)
Map:AddRoom () (at Assets/StudioDMB/Mageller/Scripts/Map.cs:41)
It ought to be a null, right? Instead there appears to be -- nothing. Unless it is a missing reference. If I have deleted a value in an element, it does return null.