- Home /
Question by
greasybear · Nov 07, 2016 at 07:41 AM ·
c#null referenceterminal
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<Inventory>();
return string.Join("\n",
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?
Comment
Answer by Ellana · Nov 07, 2016 at 10:53 PM
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("\n",
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 :)