Change to WeaponConfig

When I change the Weapon SO to WeaponConfig I get the following errors when I try and start the game as Sam did. It’s like it didn’t fix the other areas where WeaponConfig is used.

NullReferenceException: Object reference not set to an instance of an object
RPG.Combat.Fighter.AttachWeapon (RPG.Combat.WeaponConfig weaponConfig) (at Assets/Scripts/Combat/Fighter.cs:78)
RPG.Combat.Fighter.EquipWeapon (RPG.Combat.WeaponConfig weaponConfig) (at Assets/Scripts/Combat/Fighter.cs:72)
RPG.Combat.WeaponPickup.Pickup (RPG.Combat.Fighter fighter) (at Assets/Scripts/Combat/WeaponPickup.cs:31)
RPG.Combat.WeaponPickup.HandleRaycast (RPG.Control.PlayerController callingController) (at Assets/Scripts/Combat/WeaponPickup.cs:60)
RPG.Control.PlayerController.InteractWithComponent () (at Assets/Scripts/Control/PlayerController.cs:64)
RPG.Control.PlayerController.Update () (at Assets/Scripts/Control/PlayerController.cs:50)

Null reference exceptions are when you reference an object that does not exist.

Perhaps something died or something wasn’t created that needed to be.

Most likely, this is a direct result of making the name change from Weapon to WeaponConfig. Sometimes the serializer just doesn’t make the correct reference adjustments when the script name changes. Check all of your Fighters to make sure that the defaultWeaponConfig is set properly.

1 Like

I went through fighter, weaponpickup, and playercontroller to find every one of the errors. I changed everything by hand that didn’t change with using F2 and that was what I got as far as errors. I went back through and downloaded a save right before this part in the videos, and I will start again and see if I can get the errors fixed. The main parts of weaponConfig changed, but when it came to the features of the code about equipping the weapons, it gave that error. Even though it showed the correct cursor over them, the player character would not move toward any of the three weapons.

Paste in your CombatTarget.cs and we’ll take a look.

1 Like

My CombatTarget.cs as it is before the changes from the Weapon Prefab Refactor

using UnityEngine;
using RPG.Attributes;
using RPG.Control;

namespace RPG.Combat
{
    [RequireComponent(typeof(Health))]
    public class CombatTarget : MonoBehaviour, IRaycastable
    {
        public CursorType GetCursorType()
        {
            return CursorType.Combat;
        }

        public bool HandleRaycast(PlayerController callingController)
        {
            if (!callingController._fighter.CanAttack(gameObject))
            {
                return false;
            }
                
            if (Input.GetMouseButton(0))
            {
                callingController._fighter.Attack(gameObject);
            }
            return true;
        }
    }
}

My apologies, I meant to ask for the WeaponPickup.cs. I suspect, looking at the stack trace with a fresh pair of eyes, that the WeaponPickup doesn’t have it’s WeaponConfig assigned and is passing a null reference to the Fighter EquipWeapon.

1 Like

Here is the WeaponConfig.cs script. Sorry, I took extra days to get it posted Brian. This is before I do the Weapon Prefab Refactor.

using System.Collections;
using UnityEngine;
using RPG.Control;

namespace RPG.Combat
{
    public class WeaponPickup : MonoBehaviour, IRaycastable
    {
        // Serialized Variable
        [Header("WeaponConfig Info")]
        [SerializeField] Weapon _weapon;
        [Header("Respawn Time")]
        [SerializeField] float _respawnTime = 5f;
        
        // Cached Variable
        
        
        // Private Variables
        

        private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.tag == "Player")
            {
                Pickup(other.GetComponent<Fighter>());
            }
        }

        private void Pickup(Fighter fighter)
        {
            fighter.EquipWeapon(_weapon);
            StartCoroutine(HideForSeconds(_respawnTime));
        }

        private IEnumerator HideForSeconds(float seconds)
        {
            ShowPickup(false);
            yield return new WaitForSeconds(seconds);
            ShowPickup(true);
        }

        void ShowPickup(bool shouldShow)
        {
            GetComponent<Collider>().enabled = shouldShow;
            foreach (Transform child in transform)
            {
                child.gameObject.SetActive(shouldShow);
            }
        }

        public CursorType GetCursorType()
        {
            return CursorType.Pickup;
        }

        public bool HandleRaycast(PlayerController callingController)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Pickup(callingController.GetComponent<Fighter>());
            }
            return true;
        }
    }
}

Since the script is before the refactor, I might be missing what’s changed to cause an issue… that being said, assuming that this was only changing Weapon to WeaponConfig, there shouldn’t be a problem here as long as the _weapon field is set to a valid WeaponConfig… next place to look is in the WeaponConfig itself…

1 Like

Okay thanks Brian. I will start the video again and see if the other errors show up again. If they do, I’ll post the new WeaponConfig.cs file. But hopefully it was just something I missed when I refactored the code.

Hey Brian I found the error in Fighter that was calling all the problems. One of the serialized variables

[SerializeField[  WeaponConfig _defaultWeapon;

Didn’t change it’s name, but down in the script for

private WeaponConfig SetupDefaultWeapon()
{
     AttackWeapon(_defaultWeaponConfig);
    return _defaultWeaponConfig;
}

It had changed to _defaultWeaponConfig but the variable never changed for some reason. I changed both to match the variable and all of the errors went away. I can’t believe that I missed something that small when I did the small refactor (“As Sam calls it”). LOL

That refactor in particular has been tricky for many students. It caused me grief when I originally took the course because so many references didn’t get properly reserialized.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms