- Home /
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[i].name, partSlotButton))
{
placingparts.PlaceItem(cockpit[i]);
}
}
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[i].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[i].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[i].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[i].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[i].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[i].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[i].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;
}
}
}
Answer by ChristianLinnell · May 04, 2014 at 09:50 PM
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[i]);":
print("PlacingParts: " + placingparts != null);
If it prints "PlacingParts: false" that's your problem.
If it says true, check that cockpit[i] is actually set to something.
It says true ,but cockpit[i] is an array that holds game objects that I publicly assign in the inspector and i is the number in the array. How could I assign this a value.
I DIDN'T HAVE THE SCRIPT ON THE OBJECT. Thank you for showing me that line of code. It gave me some insight on debugging!
No problem! Two things to keep in $$anonymous$$d:
ALWAYS check that the component exists before you try to assign it to a variable or so something with it if (whatever.GetComponent()){ ... }
if you're stuck, use print() everywhere!! Print the value of variables, print "in loop" "out of loop" "in if block" "in else block" flags all over the place.
Answer by SeeSharp · May 04, 2014 at 09:53 PM
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[i]);
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[i] = (whatever...);
placingparts.PlaceItem(cockpit[i]);
Please feel free to ask for further assistance, and I'm sorry if I wasn't of any help.
Best of luck,
SeeSharp.