Not calling at OnStateExit()

Once my chosen enemy has finished their attacking animation, I want the player to lose some health. To do this, I am calling an int (PlayerDamage) to a different script.

public class FootmanDamage : StateMachineBehaviour {

    public GameObject Player;
    public int PlayerDamage = 25;

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        Player.GetComponent<CharHealth> ().RemoveHealth (PlayerDamage);
    }
}
public class CharHealth : MonoBehaviour {

    public int health;
    Text text;

void Awake()
    {
        text = GameObject.Find("Canvas").GetComponent<Text>();
    }

    void Start()
    {
        health = 100;
        SetHealthBarText();
    }

    public void RemoveHealth(int PlayerDamage)
    {
        health -= PlayerDamage;
        SetHealthBarText();
    }

void SetHealthBarText()
    {
        text.text = "Health: " + health.ToString();
    }
}

Not sure at what point there’s an issue. This code is very similar to code used on the enemies to be killed.

Personnaly i use event animation (beacause some times you want the damage done before the end of the animation). Hope that can help you http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

1 Like

Thanks CrymX, I would ideally have a solution given through code. I’m not used to trying out what you’re suggesting.

To check that the problem isn’t with CharHealth.cs, I just added:

if (Input.GetKey(KeyCode.B))
        {
            health -= 1;
            SetHealthBarText();
        }

And the health does decrease. Really not sure why there’s no damage taken when the enemy animates.

According to the console: Object reference not set to an intance of an object. I have no idea why, the character’s prefab is linked to it in the inspector.

can you paste the output error ?

1 Like

Error:
NullReferenceException: Object reference not set to an instance of an object
FootmanDamage.OnStateExit (UnityEngine.Animator animator, AnimatorStateInfo stateInfo, Int32 layerIndex) (at Assets/Scripts/AI/FootmanDamage.cs:21)

The function the error appertains to is:

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        Player.GetComponent<CharHealth> ().RemoveHealth (PlayerDamage);
    }

As the image above shows Player does have a GameObject attached to it, so I really don’t get why the exception is taking place.

can you paste all the class FootmanDamage ?

it’s seems like the player is not the problem

Or the gameObject named “221015” do not have component CharHealth

I really appreciate the time and feedback you’re giving to help sort out this problem. Below is the information you have requested.

FootmanDamage class:

using System.Collections;

public class FootmanDamage : StateMachineBehaviour {

    public GameObject Player;
    public int PlayerDamage = 25;

    override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        Player.GetComponent<CharHealth> ().RemoveHealth (PlayerDamage);
    }
}

Inspector of 221015 shows that Char Health is attached:

[quote]*
NullReferenceException: Object reference not set to an instance of an object
FootmanDamage.OnStateExit (UnityEngine.Animator animator, AnimatorStateInfo stateInfo, Int32 layerIndex) (at Assets/Scripts/AI/FootmanDamage.cs:21)
*[/quote]
*
In your error it’s specified line 21, i don’t understand because your script contain only 13 lines…
Maybe you have 2 scripts named CharHealth ?

I apologise. I had commented lines that I lobbed off. Line 21 is Player.GetComponent<CharHealth> ().RemoveHealth (PlayerDamage);

There is only one script named CharHealth.

Can you try this please ?

Player.SendMessage("RemoveHealth",PlayerDamage);

SendMessage RemoveHealth has no receiver
UnityEngine.GameObject:SendMessage(String, Object)

It would seem that Player is not being called, strange.

When OnStateExit is fired, are you sure your gameObject player is enabled and his script CharHealth is enabled too ?

Yes, because the camera is a child of the gameObject, and CharHealth ensures that the amount of health is displayed on the screen.

What’s happend if you do that ?

 override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      Debug.Log(Player.name);
   }

StateMachineBehaviours are a bit wonky at times.

Make sure that your Player GameObject is still linked to the state after you’ve pressed play. Something might be happening there.

In general, when you get nullpointers on a line with a long chained line of property accessors, it’s smart to split up the line to see where the nullpointer comes from:

Player.GetComponent<CharHealth>().RemoveHealth(PlayerDamage);

//change the above to:
var charHealth = Player.GetComponent<CharHealth>() //if the nullpointer happens here, Player is null
charHealth.RemoveHealth(PlayerDamage); //if the nullpointer happens here, GetComponent returned null

For my statemachines, I find it cumbersome to drag in objects from the inspector. Because of that, I fetch the relevant scripts on OnStateEnter. In your case, if the footman’s animator is on a child object of the FootMan, you can do this:

public class FootmanDamage : StateMachineBehaviour {

    private CharHealth charHealth;
    public int PlayerDamage = 25;

    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        charHealth = animator.GetComponentInParent<CharHealth>();
    }

     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
         charHealth.RemoveHealth (PlayerDamage);
     }
}

Both suggestions for amended code is coming back with errors.

 override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
      Debug.Log(Player.name);
   }

Again confirms that there’s no receiver.

override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        var charHealth = Player.GetComponent<CharHealth>();
        charHealth.RemoveHealth(PlayerDamage);

The error that appears here is

NullReferenceException: Object reference not set to an instance of an object
FootmanDamage.OnStateExit(UnityEngine.Animator animator, etc.) (at Assets/Scripts/AI/FootmanDamage.cs.16)

Line 16, being charHealth.RemoveHealth(PlayerDamage);

Finally, I tried

public class FootmanDamage : StateMachineBehaviour {
    private CharHealth charHealth;
    public int PlayerDamage = 25;
    override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
        charHealth = animator.GetComponentInParent<CharHealth>();
    }
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
         charHealth.RemoveHealth (PlayerDamage);
     }
}

And received the same error.

I do not know how helpful this images are:


Footman Damage is attached to every animation within this Animation layer with the same settings. When I attached it to the state machine, the same errors appear. The state machine has its own script which plays one of the four animations at random. This script works perfectly fine.

Where is the animator attached? my GetComponentInParent assumed that the animator is a child object of the object with the CharHealth script.

Ah. CharHealth is attached to 221015 (Player). The object using FootmanDamage is not the Player, it is attacking and trying to cause damage to the Player.

To answer your question, the Animator is attached to Footman, who I am trying to get to lower Player’s health.

Are there any other details that I could provide to shed some light over the situation?

Ah, right then. For some reason I was assuming that you were trying to do damage to the thing playing the animation, which I admit wasn’t a very smart thing to assume.

Okay, first of all, dragging the Player object in through the inspector won’t work. The player object exists in a scene, while the animator controller is independent of that scene. If you’ve dragged the players prefab into the FootmanDamage script, your script will be trying to damage the prefab object, not the copy of that prefab in the scene.

You should probably not be able to drag an object from a scene into an AnimatorStateMachineBehavior, as that would mean the behaviour would fail on any other scene.

So, to get the player. First of all, where do you trigger this animation? If that’s on a script on the same object as your footman animator, would that know about the player’s health? In that case you could store the player’s health there.

1 Like