A weird NullReferenceException that would disappear in run-time when I select the gameobject in editor's hierarchy the script attach to.
Hi, I am an Architecturual Gruaduate Student of University of Wisconsin-Milwaukee. Right now I am using an personal version of unity and encounter some scripting/engine problem I can not solve myself and can not find answers from community, and my thesis project is an Unity project! I would be very appreciate if someone can help! Thank you!
Here is the YouTube link to my reproduction of the issue (100% reproducable): Unity NullReferenceException Glitch https://www.youtube.com/watch?v=4TW18FXKn_4
Here is a link to the issue I uploaded to unity. (Case 770803) NullReferenceException on script attached to an instantiated gameobject https://fogbugz.unity3d.com/default.asp?770803_kcs7nic90r3u2q6g
Thank you, guys!
those sorts of bugs are usually a result of something uninitialized in your script, not unity's code so it's not appropriate to upload to their bug tracker.
try posting the code of the script causing the error along with the CO$$anonymous$$PLETE error message (including line number info). make sure that you format the code using the 101/010 button so that it's a) readable & b) the line numbers match the error message (so we can help you!)
public void LoadInventory()
{
Debug.Log("Inventory file folder at: " + Application.persistentDataPath);
for (int i = 0; i < 128; i++)
{
if (File.Exists(Application.persistentDataPath + "/PlayerInventory(" + i + ")Info.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerInventory(" + i + ")Info.dat", File$$anonymous$$ode.Open);
Inventory data = (Inventory)bf.Deserialize(file);
file.Close();
Debug.Log("Inventory file Loaded from: " + Application.persistentDataPath + "/PlayerInventory(" + i + ")Info.dat");
Inventory InventoryToLoad = new Inventory();
InventoryToLoad.inventoryID = data.inventoryID;
InventoryToLoad.inventoryName = data.inventoryName;
InventoryToLoad.slotAmount = data.slotAmount;
InventoryToLoad.gearAmount = data.gearAmount;
InventoryToLoad.weaponAmount = data.weaponAmount;
InventoryToLoad.potionAmount = data.potionAmount;
InventoryToLoad.carryweight = data.carryweight;
InventoryToLoad.weight = data.weight;
InventoryToLoad.query_amount = data.query_amount;
InventoryToLoad.query_dura = data.query_dura;
InventoryToLoad.query_equip = data.query_equip;
InventoryToLoad.query_id = data.query_id;
InventoryToLoad.query_slot = data.query_slot;
StartPanel(InventoryToLoad);
}
}
for (int i = 0; i < 128; i++) //check for the condition that no project exist
{
if (!File.Exists(Application.persistentDataPath + "/PlayerInventory(" + i + ")Info.dat"))
{
if (i == 127)
{
Inventory InventoryToLoad = new Inventory();
InventoryToLoad.inventoryID = 0;
InventoryToLoad.inventoryName = "$$anonymous$$y Inventory";
StartPanel(InventoryToLoad);
Debug.Log("No inventory file loaded from: " + Application.persistentDataPath + "/PlayerInventory(" + i + ")Info.dat. Initialize Empty Inventory");
}
continue;
}
else
break;
}
}
void StartPanel(Inventory inv)
{
GameObject thePanel = Instantiate(Panel) as GameObject;
thePanel.transform.SetParent(panelholder.transform);
thePanel.transform.localPosition = new Vector3(0, 0, 0);
//GameObject thePanel = panelholder.transform.GetChild(0).gameObject;
SlotPanel PaneltoLoad = thePanel.GetComponent<SlotPanel>();
PaneltoLoad.inventoryID = inv.inventoryID;
PaneltoLoad.inventoryName = inv.inventoryName;
PaneltoLoad.slotAmount = inv.slotAmount;
PaneltoLoad.gearAmount = inv.gearAmount;
PaneltoLoad.weaponAmount = inv.weaponAmount;
PaneltoLoad.potionAmount = inv.potionAmount;
PaneltoLoad.carryweight = inv.carryweight;
PaneltoLoad.weight = inv.weight;
PaneltoLoad.query_amount = inv.query_amount;
PaneltoLoad.query_dura = inv.query_dura;
PaneltoLoad.query_equip = inv.query_equip;
PaneltoLoad.query_id = inv.query_id;
PaneltoLoad.query_slot = inv.query_slot;
thePanel.name = inv.inventoryName;
}
the way I instantiate the inventory panel (which has the bug)
public void AddItem(int id, int quantity, int dura) //pick up item, gain item from NEI or other NPC, also used to reconstruct inventory when reload scene.
{
Item itemToAdd = database.FetchItemByID(id);
Debug.Log(itemlist + " :" + itemlist.Count);
for (int i = gearAmount + weaponAmount; i < gearAmount + weaponAmount + slotAmount; i++)
{
Debug.Log(itemlist[i].ID);
if (itemlist[i].ID == id)
{
the "itemlist[i]" got an NullReferenceException error
Answer by SkyArchitect · Feb 25, 2016 at 04:57 AM
NullReferenceException: Object reference not set to an instance of an object SlotPanel.AddItem (Int32 id, Int32 quantity, Int32 dura) (at Assets/My Assets/System/Item/Scripts/SlotPanel.cs:514) NEIslot.Use () (at Assets/My Assets/System/Item/Scripts/NEIslot.cs:36) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:144) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:621) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:756) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Your answer
Follow this Question
Related Questions
Script for activating next GameObject after first one has been found/tracked (Vuforia) 1 Answer
OnCollisonEnter2D Not Firing after checking collider 1 Answer
It does not work removing items from the inventory 0 Answers
How can I load/save a Prefab refference? 0 Answers
[SOLVED]Removing gameobject from list don't change the index 1 Answer