Animator setBool NullReference exception while trying to do animation when tap a button

Hi guys,

I really need help for this so I have a GameObject (Character) link to to a script (CharacterAnimateAction) with an animator inside it. I also have another Battle script (BattleScript) which inside there is the function Attack(). What I’m trying to do is to init and call the animation in BattleCript from CharacterAnimateAction script but I can’t make it work, the line anime.setBool(“isAttacking”, true) is throwing an NullReferenceException. And all of this to try to move my character position to the monster position and do the Attack animation. Can you help me out guys?

namespace Assets.StreetRPG_Core.Battle_View
{
public class BattleScripts : MonoBehaviour
{
public Battle battle;
public CharacterAnimateAction characterAnimate;

    void Start()
    {
		battle = new Battle();
		characterAnimate = new CharacterAnimateAction ();
    }

    void Update()
    {
    }

	public void PlayerAttack()
	{
		battle.game.Attack ();
		characterAnimate.getCharactersPositions ();
		characterAnimate.AttackAnimation ();
		print (battle.game.getUser().skill1["name"]);
	}

}

public class CharacterAnimateAction : MonoBehaviour {

// Use this for initialization
public Vector3 characterPosition;
public Vector3 monsterPosition;
public MonsterAnimateAnimation monsterAnimate;	
Animator anime;

// Use this for initialization
void Start () {
	anime = GetComponent<Animator> ();
	monsterAnimate = new MonsterAnimateAnimation ();
	characterPosition = transform.position;
}

// Update is called once per frame
void Update () {
	
}

public void getCharactersPositions()
{
	GameObject theCharacter = GameObject.Find ("Character");
	CharacterAnimateAction characterAnimate = theCharacter.GetComponent<CharacterAnimateAction> ();
	characterPosition = characterAnimate.characterPosition;

	GameObject theMonster = GameObject.Find ("Boss");
	monsterAnimate = theMonster.GetComponent<MonsterAnimateAnimation> ();
	monsterPosition = monsterAnimate.monsterPosition;

	characterPosition = new Vector2 (monsterPosition.x, monsterPosition.y);
}

public void AttackAnimation()
{
	anime.SetBool ("isAttacking", true);
}

}

You don’t need to use new to initialize a Monobehavior script. Instead you should use GetComponent<> to get the reference.
The problem you have is that the variable characterAnimate reference is not found yet in the class BattleScripts .
Your BattleScripts Start() Method should look something like this.

    void Start()
    {
        characterAnimate = GameObject.Find("Character").
            GetComponent<CharacterAnimateAction>();
    }