Fetching Objects from a List Always Return Null

Super super strange issue here. So I have a list, called ItemList. I can access the objects in it directly, however I cannot assign its entries to variables.

For example. Let’s say I have an item named Apple. The following then works like this:

ItemObject MyItem = ItemList[0];
string itemName = ItemList[0].itemName;

This will result in ‘MyItem’ being null, and ‘itemName’ being Apple.

Here is my exact test code:

        public static HunkItem GetItemWithName(string inName)
        {
            Debug.LogError("List Count: " + ItemList.Count);
            for (int i = 0; i < ItemList.Count; i++)
            {
                Debug.Log("List Entry " + i.ToString() + " = " + ItemList[i].ItemName);
                Debug.Log("List Matches " + string.Compare(ItemList[i].ItemName, inName));
                if (string.Compare(ItemList[i].ItemName, inName) == 0)
                {
                    Debug.Log("Returning: " + ItemList[i].ItemName);
                    return ItemList[i];
                }
            }
            return null;
        }

That code has the following console result:

List Count: 3
List Entry 0 = Apple
List Matches 0
Returning: null

Got to admit… I’m absolutely baffled here. The only thing I can think of is we just upgraded this project to Unity 2017.3.0f3, and something became broken in that version of Unity.

Any ideas?

Ah. Figured it out. Had to deal with how Unity handles MonoBehavior derived objects.

HunkItem was derived from MonoBehavior, but was not being attached to a gameObject. It was like that because I refactored the code for better data management, and those were no longer GameObjects, but instead managed within the data. Since HunkItem was still a MonoBehavior, it existed, and it worked, and it stored data, but an object for it didn’t exist because of the way Unity handles GameObjects. I removed the MonoBehavior and just made it a standard .NET class and all works.

Always after we post that we find the solution. :slight_smile:

2 Likes

Guaranteed. Anytime I get stuck anymore I just start asking everyone for help, and then I figure it out before anyone answers. That’s my method, lol.

2 Likes

Haha ye, when you sit there almost smashing your head against everything and then you’re about to ask someone or complain about it… “[…] I’m not sure why and it’s freaking me out, but this just doesn’t seem to work as expected when I… OH! nevermind, see you”
Then you just walk away and fix it in seconds. :smile:

1 Like

I have a friend I use for this. He doesn’t even know anything about programming, and I’m sure it annoys him, but while explaining the problem I inevitably figure out the solution. Part of me thinks this is why Slack includes a way to talk to yourself.

In the Sherlock television show in the states (Elementary), Sherlock uses a phrenology bust for exactly this. Since I saw that, I call my friend Angus.

Walking in the shoes of giants… or was it standing on the shoulders? Whatever.

3 Likes

I often talk to one of my cats like this… which is appropriate as I’m producing a game about cat potatoes.

It’s actually a technique called rubber duck debugging whereby explaining the problem to a third party helps us find a solution. Pretty neat from a psychological point of view.

2 Likes

Don’t let Lysander’s friend see this post, lol.

1 Like