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);
}
}
Answer by Pengocat · Jan 13, 2017 at 03:28 AM
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>();
}
Hi thank you it worked that way! The animation attack launch successfully thank you so much! I'm also trying to move my character game object to my monster game object:
characterPosition = new Vector2 (monsterPosition.x, monsterPosition.y);
it worked but on the screen the character game object didn't move and stay at the same position. Can you help me with this please?
The character position variable should be of type Transform and not Vector3 if you want to use it like that.
public Transform characterPosition;
And then you set it by...
characterPosition.position = characterAnimate.characterPosition;
In fact you may end up setting the same target to it's own value? It is a bit confusing the way you have things organised.
Thank you for your reply! I just find out that characterPosition = transform.position doesn't work when we change characterPosition. But when I'm changing directly transform.position value it works!
Your answer

Follow this Question
Related Questions
Object reference not set to an instance of an object (Unity 2D animation) 1 Answer
8-way 2D top down movement (diagonal) idle animation issues 0 Answers
2D animation transitions and unexpected position changes 2 Answers
2D Animation Delay 3 Answers
Simple 2D movement with animation while you are moving 0 Answers