Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.
Forum begins after the advertisement:
[Part 10] Can’t add new weapon
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 10] Can’t add new weapon
- This topic has 4 replies, 2 voices, and was last updated 1 year, 9 months ago by
Terence.
-
AuthorPosts
-
January 14, 2024 at 5:32 pm #13080::
I got this error when adding new weapon:
NullReferenceException: Object reference not set to an instance of an object InventoryManager.AddWeapon (System.Int32 slotIndex, WeaponController weapon) (at Assets/Script/Player/InventoryManager.cs:51) PlayerStats.SpawnWeapon (UnityEngine.GameObject weapon) (at Assets/Script/Player/PlayerStats.cs:325) InventoryManager+<>c__DisplayClass18_0.<ApplyUpgradeOptions>b__0 () (at Assets/Script/Player/InventoryManager.cs:153)Level up weapon, adding new passive item and level up passive item work fine but i got that error choosing new weapon. The errors point at InventoryManager
weaponLevels[slotIndex] = weapon.weaponData.Level;PlayerStat
inventory.AddWeapon(weaponIndex, spawnedWeapon.GetComponent<WeaponController>()); // Add weapon to its inventory slotInventoryManager
upgradeOption.upgradeButton.onClick.AddListener(() => player.SpawnWeapon(chosenWeaponUpgrade.initialWeapon)); //Apply the button functionalityJanuary 14, 2024 at 10:05 pm #13081January 14, 2024 at 10:09 pm #13082::Just to add on, the issue should be in
AddWeapon()because it is the first line in the stack track.The Null Reference exception means that one of the items in that line has a null variable that you are trying to access the property of. There are 3 variables there you try to access the property or method of, and one of them is null and is causing this issue:
inventory: If it is null, theninventory.AddWeapon()fails.spawnedWeapon: If this is null, you will not be able to doGetComponent()with it.GetComponent<WeaponController>(): Finally, if this is null, this may cause issues wheninventory.AddWeapon()tries to use the variable.
January 15, 2024 at 2:41 am #13091::I figured it out, I put the weapon prefab instead of weapon controller in the Initial Weapon field, I need to get my eyes checked lol, thanks for the help
January 17, 2024 at 10:59 am #13111 -
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below:
Get 25% off your next order from the Unity Asset Store: YW86GYBU8S.