#What are the values inside an empty array element?

1 messages · Page 1 of 1 (latest)

twilit root
#

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.

last coyote
#

a list doesn't really have "empty elements"

twilit root
#

yeah, it appears to have a Room(none)

#

Which isn't really helpful shakes fist at Unity

#

I am missing something.

last coyote
#

I believe that an invalid unity object reference will turn into an empty string

#

or was it a genuine null ? i forget

#

An invalid unity object reference is still an actual object -- it's not literally a null

#

It's a bit misleading

twilit root
#

hmm. How do I check for whatever it is?

#

I will try a string.

last coyote
#

are you having a problem right now?

twilit root
#

Yes

last coyote
#

you've just said that it logs "" instead of "null"

twilit root
#

I think you are right, Unity is putting a placeholder in there, it just isn't very good at explaining what it is.

last coyote
#

oh, yes, it's serialized

#

that's why

#

If you had a list that was not serialized, and Unity wasn't messing with it, it'd be genuine nul

twilit root
#

It isn't an empty string.

twilit root
#

I am tempted to say you are correct, but I can't really tell now. lol

#

ugh and it needs to be a ScriptableObject, not a class. lol

#

@last coyote Thank you for your help!

#
CreateAssetMenu attribute on Map will be ignored as Map is not derived from ScriptableObject.
UnityEditor.AttributeHelper:ExtractCreateAssetMenuItems ()

If you are curious.

last coyote
last coyote
#

(that allows it to give a more specific error if you try to use these invalid items)

twilit root
last coyote
#

ah, I see

twilit root
#

It's a completely different problem on the same line of code. 😓