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

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #15813
    Boby
    Level 3
    Participant
    Helpful?
    Up
    0
    ::

    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

    #16564
    A_DONUT
    Level 7
    Moderator
    Helpful?
    Up
    1
    ::

    The NullReferenceException indicates that something you’re trying to access hasn’t been initialized (i.e., it’s null). Based on the errors, it seems that currentStats or owner in Weapon.cs are likely not initialized before the Update() method is called.

    Common Causes of This Error:

    1. Initialization Order Issue: Since you’ve removed the Start() and Awake() methods, the object might not have all its properties set up when Update() is called.
    2. Dependency Missing: currentStats or owner might depend on values set during initialization that are now skipped.

    Steps to Fix:

    1. Check currentStats and owner:

      • Ensure these fields are being assigned values properly in Initialise(data).
      • Add null checks to prevent errors if the values aren’t ready.
    2. 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>
    3. Ensure Initialise(data) Completes Before Update():

      • Verify that Initialise(data) correctly sets currentStats and owner.
      • You can also add logging inside Initialise() to ensure it’s called before Update().
    4. Reconsider Removal of Start() or Awake():

      • You might need to keep minimal initialization in Awake() or Start(). 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>

    Check in ProjectileWeapon.cs:

    • Since ProjectileWeapon extends Weapon, ensure base.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 of Update():

    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 before Update() and ensure proper setup.

    has upvoted this post.
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: