Choosing attacker behavior by current animation

So I’m attempting to set up my attacker so that the first defender it touches, it jumps over, then it enters a crawling animation and attacks everything it comes into contact afterwards normally. I’m having a little trouble figuring out the code though. Here’s what I’ve got so far

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

public class Jackhammer : MonoBehaviour
{
    Animator jhanimator;

    private void OnTriggerEnter2D(Collider2D otherCollider)
    {
    GameObject otherObject = otherCollider.gameObject;

        if (otherObject.GetComponent<Defender>() && jhanimator.GetCurrentAnimatorStateInfo(0).IsName("JHRun"))
        {
            GetComponent<Animator>().SetTrigger("jumpTrigger");
        }
        else if (otherObject.GetComponent<Defender>() && jhanimator.GetCurrentAnimatorStateInfo(0).IsName("JHCrawl"))
        {
            GetComponent<Attacker>().Attack(otherObject);
        }

    }
}

My plan was to run two if statements where it’ll look at what the attacker’s current animation state is when it bumps into a defender and either jump if it’s doing “JHrun” or attack if it’s doing “JHCrawl”. So far though, the only result is the jump won’t trigger and gives me a null reference exception error. Any help would be greatly appreciated!

Hi Ben,

Your code looks fine. The only flaws I see is that it checks for the Defender component twice, and the Animator and/or Attacker component might get looked for multiple times during runtime. You could cache the Animator and the Attacker object.

Apart from that, your code makes sense.

NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring.

Where does the Jackhammer object get the reference to the Animator from that is supposed to be assigned to the jhanimator variable?

It looks like the line of code the null reference is pointing to is this one

GameObject otherObject = otherCollider.gameObject;

I’m not sure why that’s the case because I assume this is just getting info on wether the other colliders class is and it works on my other attacker prefab.

Also maybe I’m confused, but I thought when I cached Animator jhanimator; at the top that should access the animator on the game object the script is attached to.

Okay, so good news on my progress, I’ve got the enemy behavior acting exactly like I want it to! I think my only problem is I’m missing something about caching get components. Right now I’m just calling them every time and I know that’s eating memory. I think I’m doing it right by putting the name I want to cache = getcomponent in the start method, but it’s saying the name doesn’t exist in the curent context. What am I doing wrong?

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

public class Jackhammer : MonoBehaviour
{
    private void Start()
    {
        jhanimator = GetComponent<Animator>();
    }

    private void OnTriggerEnter2D(Collider2D otherCollider)
    {
    GameObject otherObject = otherCollider.gameObject;

        if (otherObject.GetComponent<Defender>() && GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("JHRun"))
        {
            GetComponent<Animator>().SetTrigger("jumpTrigger");
        }
        else if (otherObject.GetComponent<Defender>() && GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("JHCrawl"))
        {
           GetComponent<Attacker>().Attack(otherObject);
        }

    }
}

You forgot to declare jhanimator as a member variable.

    Animator jhanimator; // <-- You are missing this!
    private void Start()
    {
        jhanimator = GetComponent<Animator>();
    }

Ah! I see now. I thought I tried that earlier but I must have mistyped it or something. Thanks! For posterity, here’s the finished script.

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

public class Jackhammer : MonoBehaviour
{
    Animator jhanimator;
    Attacker attacker;

    private void Start()
    {
        jhanimator = GetComponent<Animator>();
        attacker = GetComponent<Attacker>();
    }

    private void OnTriggerEnter2D(Collider2D otherCollider)
    {
    GameObject otherObject = otherCollider.gameObject;

        if (otherObject.GetComponent<Defender>() && jhanimator.GetCurrentAnimatorStateInfo(0).IsName("JHRun"))
        {
            jhanimator.SetTrigger("jumpTrigger");
        }
        else if (otherObject.GetComponent<Defender>() && jhanimator.GetCurrentAnimatorStateInfo(0).IsName("JHCrawl"))
        {
           attacker.Attack(otherObject);
        }

    }
}
1 Like

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

Privacy & Terms