Null reference from my sword

I keep getting a null reference once in a while. i cant duplicate it all the time i think it’s because of the swing animation classes.

MissingReferenceException: The object of type ‘GameObject’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Sword.SwingUpFlipAnimEvent () (at Assets/Scripts/Weapons/Sword.cs:52)

when I swing and in the middle of the swing while the animation is going and I flip the character around with my mouse I sometimes get this error.

Let’s start with a look at your SwingUpFlipAnimEvent() method in Sword.cs. Be sure and mark line 52.



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sword : MonoBehaviour, IWeapon
{
    [SerializeField] private GameObject slashAnimationPrefab;
    [SerializeField] private WeaponInfo weaponInfo;

    private Transform slashAnimationSpawnPoint;

    [SerializeField] private Transform weaponCollider;

    private Animator myAnimator;
    private string swordAttack = "Attack"; //name of the trigger on the animation controller is called "Attack" under parameters. 

    private GameObject slashAnimation;

    private void Awake()
    {
        myAnimator = GetComponent<Animator>();
    }

    private void Start()
    {
        slashAnimationSpawnPoint = PlayerController.Instance.GetSlashAnimationSpawnPoint();
    }

    public void Attack()
    {    
        //fire sword animation
        myAnimator.SetTrigger(swordAttack);
        weaponCollider.gameObject.SetActive(true);
        // take the parent object that is the sword and get the slash to be on the sword.
        slashAnimation = Instantiate(slashAnimationPrefab,slashAnimationSpawnPoint.position,Quaternion.identity);
        slashAnimation.transform.parent = this.transform.parent;    
    }

    public void DoneAttackingAnimationEvent()
    {
       weaponCollider.gameObject.SetActive(false);
    }

    public void SwingUpFlipAnimEvent()
    {
        slashAnimation.gameObject.transform.rotation = Quaternion.Euler(-180,0,0);  //*************  Line 52
        if (PlayerController.Instance.FacingLeft2)
        {
            slashAnimation.GetComponent<SpriteRenderer>().flipX = true;
        }
    }

    public void SwingDownAnimEvent()
    {
        slashAnimation.gameObject.transform.rotation = Quaternion.Euler(0,0,0);
        if (PlayerController.Instance.FacingLeft2)
        {
            slashAnimation.GetComponent<SpriteRenderer>().flipX = true;
        }
    }

    private void MouseFollowWithOffset()
    {
        //make the sword in fromnt on the player
        //Get the Mouse position.
        Vector2 mousepos = Input.mousePosition;
        float swordAngle = Mathf.Atan2(mousepos.y,mousepos.x) * Mathf.Rad2Deg;
        //Get the players position from the camera.
        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(PlayerController.Instance.transform.position);
        if(mousepos.x < playerScreenPoint.x)
        {
            ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0,-180,swordAngle);
            weaponCollider.transform.rotation = Quaternion.Euler(0,-180,0);
        }
        else
        {
           ActiveWeapon.Instance.transform.rotation = Quaternion.Euler(0,0,swordAngle);
           weaponCollider.transform.rotation = Quaternion.Euler(0,0,0);
        }
    }

    // Update is called once per frame

    void Update()
    {
        MouseFollowWithOffset();
        weaponCollider.transform.rotation = this.transform.rotation;
    }

    public WeaponInfo GetWeaponInfo()
    {
        return weaponInfo;
    }
}

This is an odd one, as there’s no reason that slashAnimation should ever be false, as it’s instantiated in Awake(), and there’s nothing within the script that would destroy the GameObject.

That being said, null checking is always a good idea, even when we can Assert that something is not null based on code logic. You can eliminate the error by adding

if(slashAnimation == null) return;

to the beginning of the method. The only problem is that this isn’t really a solution because then the object isn’t flipped and if the GameObject really is destroyed, the error will just pop up the next time you swing the sword, so we might want to save this as a last resort.
Can you show the slashAnimationPrefab’s instpector, fully expanded.
Also, when the error message fires, pause the game and see if the animation is actually missing. This may simply be a Unity Bug…

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

Privacy & Terms