Forum begins after the advertisement:
Part 18 – Errors when removing the data from the Weapon
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › Part 18 – Errors when removing the data from the Weapon
- This topic has 1 reply, 2 voices, and was last updated 1 month ago by A_DONUT.
-
AuthorPosts
-
September 12, 2024 at 2:19 am #15813::
Hello,
On the Part 18 around 51:40, when I put the spawnedWeapon.Initialise(data) before the spawnedWeapon.OnEquip() and delete the Start and Awake in the Weapon, I’ve got two errors, running in a loop when I start the game :
NullReferenceException: Object reference not set to an instance of an object Weapon.Update () (at Assets/Scripts/Weapons/Weapons Sysetm/Weapon.cs:71) ProjectileWeapon.Update () (at Assets/Scripts/Weapons/Weapons Sysetm/ProjectileWeapon.cs:9)
Pointing on thoses code : Weapon.cs 71 protected virtual void Update(){ currentCooldown -= Time.deltaTime;
if(currentCooldown <= 0) //Define the number of projectile to instantiate <strong>Shoot(currentStats.number + owner.Stats.amount); </strong> here }
ProjectileWeapon.cs 9 protected override void Update(){ base.Update(); here
if(currentShootInterval > 0){ currentShootInterval -= Time.deltaTime; if(currentShootInterval <= 0) Shoot(currentShootCount); } }
Thank you
November 27, 2024 at 7:48 pm #16564::The
NullReferenceException
indicates that something you’re trying to access hasn’t been initialized (i.e., it’snull
). Based on the errors, it seems thatcurrentStats
orowner
inWeapon.cs
are likely not initialized before theUpdate()
method is called.Common Causes of This Error:
- Initialization Order Issue: Since you’ve removed the
Start()
andAwake()
methods, the object might not have all its properties set up whenUpdate()
is called. - Dependency Missing:
currentStats
orowner
might depend on values set during initialization that are now skipped.
Steps to Fix:
-
Check
currentStats
andowner
:- Ensure these fields are being assigned values properly in
Initialise(data)
. - Add null checks to prevent errors if the values aren’t ready.
- Ensure these fields are being assigned values properly in
-
Add Safe Checks in
Update()
:csharp protected virtual void Update(){ if (currentStats == null || owner == null) return; // Prevents errors if uninitialized currentCooldown -= Time.deltaTime; if(currentCooldown <= 0) Shoot(currentStats.number + owner.Stats.amount); } <code></code>
-
Ensure
Initialise(data)
Completes BeforeUpdate()
:- Verify that
Initialise(data)
correctly setscurrentStats
andowner
. - You can also add logging inside
Initialise()
to ensure it’s called beforeUpdate()
.
- Verify that
-
Reconsider Removal of
Start()
orAwake()
:- You might need to keep minimal initialization in
Awake()
orStart()
. For example:csharp void Awake() { // Default or safety values currentStats = new WeaponStats(); // Replace with appropriate default currentCooldown = 1.0f; // Or some default cooldown } <code></code>
- You might need to keep minimal initialization in
Check in
ProjectileWeapon.cs
:-
Since
ProjectileWeapon
extendsWeapon
, ensurebase.Update()
is only called after proper initialization.csharp protected override void Update() { if (currentStats == null || owner == null) return; base.Update(); // Rest of the code } <code></code>
Debugging Tip:
Add debug logs inside
Initialise(data)
and at the beginning ofUpdate()
:csharp void Initialise(WeaponData data) { Debug.Log("Initialise called, data: " + data); // Set up currentStats and owner here } protected virtual void Update() { Debug.Log("Update called"); } <code></code>
This should help you trace whether
Initialise()
runs beforeUpdate()
and ensure proper setup.has upvoted this post. - Initialization Order Issue: Since you’ve removed the
-
AuthorPosts
- You must be logged in to reply to this topic.