- Home /
And the bug was not very related with my script in the end, but with a dumb mistake.
Getting variable from animation returns Null
Hello, This is my first time asking a question in here and in general, therefore I apologize if I couldn't explain myself properly and I ask for understanding. With this said, I am very thankful to anyone who tries to help me solve this issue!
I am designing a game where the enemes will walk towards the player and, on contact, attack it and deal damage. Since I have to time when the attack takes place, I decided to write this logic within the animation script (as you'll see, please also tell me if it's better to do everything in the Enemy controller script instead).
Within the Enemy class, I have a public variable
public GameObject target;
and I do not define it in the inspector, since it will be given a value once it collides with a target (for now it's only the player controller). When they collide, target gets a value. So, to the important part:
public GameObject GetTarget(){
print(target); //Prints the game object of the target correctly.
return target;
}
When the Enemy is in contact with its target, it prints the correct target, however, when the value in the animation script that I get is Null.
So, here you go, this is the animation script:
public class EnemyAttack1HAnimation : StateMachineBehaviour {
public float attackDelay;
private float nextAttack;
private GameObject EnemyObject;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
nextAttack = Time.time + attackDelay;
EnemyObject = animator.gameObject.transform.parent.gameObject;
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
if (Time.time > nextAttack){
nextAttack = Time.time + attackDelay;
PlayerController target = EnemyObject.GetComponent<Enemy>().GetTarget().GetComponent<PlayerController>();
Debug.Log(target); //Null <- This is the issue (Also Null if I don't call the GetComponent, of course)
}
}
// OnStateExit is called when a transition ends and the state machine finishes evaluating this state
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
animator.SetBool("attack", false);
}
Thanks again for your help and I'm looking forward to learning further about Unity and C#! =)
$$anonymous$$ind of a dumb question here, but your player object does have a PlayerController component on it, right?
Yes, it is attached, but thanks to your comment I checked something out and indeed the problem was related to it. The thing is, the game is a tower defense, and the Enemy is colliding with the Tower, not the player (the character on the tower). Obviously, the tower has no player controller script whatsoever. So, I did such a dumb mistake of overseeing that. I feel so stupid now.
Thanks for your reply, since it helped me out of my tunnel vision =)
Follow this Question
Related Questions
Please Help!! NullReferenceException when clicking restore default pose 0 Answers
NullReferenceException on animation clips 3 Answers
Can't get boolean of an animator from other class 0 Answers
Why am I getting a NullReferenceException while trying to set a gameobjects animation frame? 0 Answers
nullReferenceException error 2 Answers