NullReferenceException: Object reference not set to an instance of an object

When I try to call this function from another script that I have everything is fine until I test the game and it says the above error. Here’s the full thing.NullReferenceException: Object reference not set to an instance of an object
BuildingMenu.OnGUI () (at Assets/Scripts/BuildingMenu.cs:53)
Can anyone tell me what the problem is?

using UnityEngine;
using System.Collections;

public class BuildingMenu : MonoBehaviour 
{
	public GameObject[] cockpit;
	public GameObject[] control;
	public GameObject[] propulsion;
	public GameObject[] fuelContainers;
	public GameObject[] aerodynamics;
	public GameObject[] structural;
	public GameObject[] utilities;
	public GameObject[] research;

	public Texture2D partSelectionBackground;
	public GUIStyle partSlotButton;
	public GUIStyle tabButton;

	private bool cockpitWindowOpen;
	private bool controlWindowOpen;
	private bool propulsionWindowOpen;
	private bool fuelContainersWindowOpen;
	private bool aerodynamicsWindowOpen;
	private bool structuralWindowOpen;
	private bool utilitiesWindowOpen;
	private bool researchWindowOpen;

	public static int cockpitAmounts;
	private int cols = 3;
	public int toolBarInt;
	private PlacingParts placingparts;

	void Start () 
	{
		cockpitAmounts = 0;
		toolBarInt = 1;
		placingparts = GetComponent<PlacingParts> ();
	}
	void Update () 
	{
		ToolBarInt ();
	}
	void OnGUI()
	{
		//THE WINDOW FOR THE COCKPITS
		if(cockpitWindowOpen == true)
		{
			GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
				for (int i = 0; i< cockpit.Length; i++)
					{
						if(GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),cockpit*.name, partSlotButton))*
  •  				{*
    

_ placingparts.PlaceItem(cockpit*);_
_
}_
_
}_
_
GUI.EndGroup ();_
_
}_
_
//THE WINDOW FOR THE Control*_
* if(controlWindowOpen == true)*
* {*
* GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);*
* for (int i = 0; i< control.Length; i++)*
* {*
_ GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),control*.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE PROPULSION*
* if(propulsionWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< propulsion.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),propulsion.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE PROPULSION*

* if(fuelContainersWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< fuelContainers.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),fuelContainers.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE AERODYNAMICS*

* if(aerodynamicsWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< aerodynamics.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),aerodynamics.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE STRUCTURAL*

* if(structuralWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< structural.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),structural.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE UTILITIES*

* if(utilitiesWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< utilities.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),utilities.name, partSlotButton);
}
GUI.EndGroup ();
}
//THE WINDOW FOR THE RESEARCH*

* if(researchWindowOpen == true)
{
GUI.BeginGroup (new Rect (0, 32, 565, 590), partSelectionBackground);
for (int i = 0; i< research.Length; i++)
{
GUI.Button (new Rect((i % cols) * 91 + 10, ((int)i / cols) * 91 + 10, 81,81),research.name, partSlotButton);
}
GUI.EndGroup ();
}
WindowTabs ();
}
void WindowTabs()
{
if(GUI.Button(new Rect(0,0,32,32),“C”, partSlotButton))
{
toolBarInt = 1;
}
if(GUI.Button(new Rect(32,0,32,32),“Co”, partSlotButton))
{
toolBarInt = 2;
}
if(GUI.Button(new Rect(64,0,32,32),“P”, partSlotButton))
{
toolBarInt = 3;
}
if(GUI.Button(new Rect(96,0,32,32),“FC”, partSlotButton))
{
toolBarInt = 4;
}
if(GUI.Button(new Rect(128,0,32,32),“A”, partSlotButton))
{
toolBarInt = 5;
}
if(GUI.Button(new Rect(160,0,32,32),“S”, partSlotButton))
{
toolBarInt = 6;
}
if(GUI.Button(new Rect(192,0,32,32),“U”, partSlotButton))
{
toolBarInt = 7;
}
if(GUI.Button(new Rect(224,0,32,32),“R”, partSlotButton))
{
toolBarInt = 8;
}*_

* }*
* void ToolBarInt()*
* {*
* if (toolBarInt == 1)*
* {*
* cockpitWindowOpen = true;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 2)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = true;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 3)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = true;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 4)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = true;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 5)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = true;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 6)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = true;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 7)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = true;*
* researchWindowOpen = false;*
* }*
* if (toolBarInt == 8)*
* {*
* cockpitWindowOpen = false;*
* controlWindowOpen = false;*
* propulsionWindowOpen = false;*
* fuelContainersWindowOpen = false;*
* aerodynamicsWindowOpen = false;*
* structuralWindowOpen = false;*
* utilitiesWindowOpen = false;*
* researchWindowOpen = true;*
* }*

* }*
}

Most probably, since the loop is starting, your gameobject doesn’t have a PlacingParts component attached to it.

Add the following line before “placingparts.PlaceItem(cockpit*);”:*
print("PlacingParts: " + placingparts != null);
If it prints “PlacingParts: false” that’s your problem.
If it says true, check that cockpit is actually set to something.

Hi,

Well, if you’re asking what the problem is, the error tells you what it is. If you’re asking for further explanation about what that means, then it means that

placingparts.PlaceItem(cockpit*);*
was probably not assigned a value.
If you’re looking for help on how to fix it, or find out what’s wrong, I can only provide you with limited support as I do not know how you are setting that value.
You could try to see what’s wrong by setting the value right before you’re trying to use it. If you do that, and that gets rid of your error, then that means that you didn’t set the value correctly/it wasn’t set at all.
Just to recap:
cockpit = (whatever…);
placingparts.PlaceItem(cockpit*);*
Please feel free to ask for further assistance, and I’m sorry if I wasn’t of any help.
Best of luck,
SeeSharp.