manojl
November 14, 2019, 8:35pm
1
I need help with this mistake please…
is part of an ammo system, unload.
error occurs while trying to empty the gun
NullReferenceException: Object reference not set to an instance of an object
UltimateSurvival.GUISystem.ItemUnload.On_UnloadClicked () (at Assets/Ultimate Survival/Scripts/ItemUnload.cs:98)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at C:/Program Files/Unity/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
My Script…
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace UltimateSurvival.GUISystem
{
public class ItemUnload : GUIBehaviour
{
//Get the main ItemInspector
// chama a função loadammo usando o item no slot atual
private ItemInspector itemInspector;
//Audio for Reload
// Áudio para recarregar
[SerializeField]
[Tooltip("Sounds that will play when trying to unload")]
private SoundPlayer m_UnloadAudio;
private void Awake()
{
itemInspector = transform.GetComponentInParent<ItemInspector>();
}
// empty the weapon
// esvazie a arma
public void emptyAmmo(SavableItem weaponItem, int ammoStack)
{
if (weaponItem.HasProperty ("Ammo") && weaponItem.HasProperty ("Ammo Type") && weaponItem.HasProperty ("Ammo Capacity"))
{
//put ammo from the gun in the maggazine
// coloca munição da arma no maggazine
var m_AmmoAmount = weaponItem.GetPropertyValue ("Ammo");
var ammo = m_AmmoAmount.Int;
ammo.Current = ammoStack;
m_AmmoAmount.SetValue (ItemProperty.Type.Int, ammo);
}
}
//Set capacity of itemInspector
// Definir capacidade do itemInspector
public void setCapacity(SavableItem ammoMagItem, int ammoCapacity){
//set the magazine capacity
// define a capacidade do magazine
if (ammoMagItem.HasProperty ("Ammo Capacity")){
var m_AmmoCapacity = ammoMagItem.GetPropertyValue ("Ammo Capacity");
var AmmoCapacity = m_AmmoCapacity.Int;
AmmoCapacity.Current = ammoCapacity;
m_AmmoCapacity.SetValue (ItemProperty.Type.Int, AmmoCapacity);
}
}
//Create ammo magazine
// Criar revista de munição
public bool unloadMag(string magName, string ammoType, int ammoStack, int ammoCapacity, out SavableItem savableItem)
{
savableItem = null;
SavableItem ammoMagItem;
ItemData ItemData;
//find the item by its namespace
// localiza o item pelo seu namespace
InventoryController.Instance.Database.FindItemByName (magName, out ItemData);
//only 1 magazine in a stack
// apenas uma magazine em uma pilha
ammoMagItem = new SavableItem (ItemData, 1);
ammoMagItem.CurrentInStack = 1;
if (ammoMagItem.HasProperty ("Ammo") && ammoMagItem.HasProperty ("Ammo Type") && ammoMagItem.HasProperty ("Ammo Capacity")) {
//put ammo from the gun un the magazine
// coloca munição da arma na magazine
emptyAmmo (ammoMagItem, ammoStack);
//set the ammo type
// define o tipo de munição
var m_AmmoType = ammoMagItem.GetPropertyValue ("Ammo Type");
m_AmmoType.SetValue (ItemProperty.Type.String, ammoType);
//set the magazine capacity
// define a capacidade do magazine
setCapacity(ammoMagItem, ammoCapacity);
//output the magazine
// produz a magazine
return savableItem = ammoMagItem;
} else {
return false;
}
}
// UNLOAD ITEM >>
// DESCARREGAR ITEM >>
public void On_UnloadClicked()
{
bool wasUnloaded = false;
var item = itemInspector.InspectedSlot.CurrentItem;
SavableItem ammoMagItem;
//just make sure that item has the properties we will use the IF statement
// apenas certifique-se de que o item tenha as propriedades, usaremos a instrução IF
if (item.HasProperty("Ammo Type") && item.HasProperty("Ammo") && item.HasProperty("Ammo Capacity"))
{
//"Ammo Magazine" is the generic magazine item
// "magazine Munição" é o item genérico da magazine
string magName = "Ammo Magazine";
string ammoType = item.GetPropertyValue ("Ammo Type").String;
//how much ammo is in the item
// quanta munição há no item
int ammoStack = item.GetPropertyValue ("Ammo").Int.Current;
//get the current capacity of the item
// obtém a capacidade atual do item
int ammoCapacity = item.GetPropertyValue ("Ammo Capacity").Int.Current;
//check if the item requires an ammo mag, which mean it is likely a weapon
// verifica se o item requer uma munição, o que significa que provavelmente é uma arma
if (item.HasProperty ("Requires Ammo Mag")) {
if (item.HasProperty ("Has Ammo Mag") && item.GetPropertyValue ("Has Ammo Mag").Bool) {
//flag that the magazine has been ejected from the weapon
// sinaliza que a revista foi ejetada da arma
var hasAmmo = item.GetPropertyValue ("Has Ammo Mag");
hasAmmo.SetValue (ItemProperty.Type.Bool, false);
//empty ane ammo from the weapon
// esvazie munição ane da arma
emptyAmmo (item, 0);
//can use generic ammo magazine or specific
// pode usar uma revista de munição genérica ou específica
unloadMag (magName, ammoType, ammoStack, ammoCapacity, out ammoMagItem);
itemInspector.m_InventoryContainer.TryAddItem (ammoMagItem);
//set that the item was unloaded
// define que o item foi descarregado
wasUnloaded = true;
}
}
//if item doesnt require an ammo mag
// se o item não exigir uma munição
else if (ammoStack > 0 && wasUnloaded == false) //only upload ammo mag if it has ammo // só carrega munição de munição se ela tiver munição
{
//unload ammo that match what was in the magazine
// descarrega munição que corresponde ao que estava na revista
itemInspector.m_InventoryContainer.TryAddItem (ammoType, ammoStack);
//empty ane ammo from the weapon
// esvazie munição ane da arma
emptyAmmo (item, 0);
//check if it is an ammo mag itself
// verifica se é uma própria munição
if (item.HasProperty ("Is Ammo Mag")) {
//if the item is amm the remove the original item in the slot
// se o item for amm, remova o item original no slot
itemInspector.InspectedSlot.ItemHolder.SetItem (null);
//can use generic ammo magazine or especific
// pode usar uma revista de munição genérica ou específica
unloadMag (magName, ammoType, 0, ammoCapacity, out ammoMagItem);
itemInspector.m_InventoryContainer.TryAddItem (ammoMagItem);
}
//set that the item was unloaded
// define que o item foi descarregado
wasUnloaded = true;
}
}
//get the name of the object, if it has a displayname the use it
// obtém o nome do objeto, se ele tiver um nome de exibição, use-o
string itemName;
if (item.ItemData.DisplayName.Length > 0 && item.ItemData.DisplayName != null) {
itemName = item.ItemData.DisplayName;
} else {
//else use its item name // use o nome do item
itemName = item.ItemData.Name;
}
//only show message that ammo was unloaded if the bool is true
// mostra apenas a mensagem de que a munição foi descarregada se o bool for verdadeiro
if (wasUnloaded) {
MessageDisplayer.Instance.PushMessage (string.Format ("<color=yellow{0}</color> Has been unloaded", itemName));
m_UnloadAudio.Play2D ();
} else {
MessageDisplayer.Instance.PushMessage (string.Format ("<color=yellow{0}</color> Has nothing to unload", itemName));
}
}
}
}
Show the code using the line numbers in the error text, specifically the code at Assets/Ultimate Survival/Scripts/ItemUnload.cs:98 (line 98)
1 Like
Null reference exception means you are using a variable that doesn’t have a value.
Please post code using code tags, not as a quote. If your error message references a specific line of code, always make it clear which line that is in the code that you post.
2 Likes
You’ll want to ensure that itemInspector isn’t null on line 23, Debug.Log it! Tips for new Unity users
1 Like
Either itemInspector is null, OR itemInspector.InspectedSlot is null.
1 Like
manojl
November 14, 2019, 10:10pm
7
hi guys I got the mistake, it was just my carelessness … thank you very much <3