Can anyone help me as to why this is a null reference?

else if (s.Equals(“magic”))
{
GameObject fps = GameObject.FindWithTag(“Player”);
Inventory inventory = fps.GetComponent();
return string.Join("
",
inventory.cliObjects.Select(edit => edit.name).ToArray());

My player objects adds public objects to the script Inventory. I have the above code on an interactive terminal that should list these objects upon typing “magic”. All that is coming up is a null reference. Can anyone help with why that is? Am I searching for the objects incorrectly?

Have you tried using Debug.log to see if you actually catch something, and know which one returns a null? You could do something like:

 else if (s.Equals("magic"))
             {
                  GameObject fps = GameObject.FindWithTag("Player");
Debug.log("Player object is " + fps);
                   Inventory inventory = fps.GetComponent<Inventory>();  
Debug.log("Inventory is "+ inventory);
                   return string.Join("

",
inventory.cliObjects.Select(edit => edit.name).ToArray());

That should give you an idea of what is actually going on, if it’s the fps object that is null, or inventory :slight_smile: