After defender is destroyed, attackers do not resume walking

After I entered the
if (!currentTarget) {
animator.SetBool(“isAttacking”, false);
}

line into my script I started getting
NullReferenceException: Object reference not set to an instance of an object
Attacker.Update () (at Assets/Scripts/Attacker.cs:21)
messages in the console and the attackers do not resume walking after they destroy an object.
I have the isAttacking bool set up in the animator and it is set up as true for the transition from walking to attacking and false for the transition back to walking.
I have checked all of the scripts through github to see that I have written the code correctly and I have gone back and watched lecture 150 through 161 again to see if I have missed anything and yet I am still stuck. Feeling a little lost in the Glitch Garden Arggggggggg!!!
Any help would be appreciated.

What is the code on line 21 of the attackers script specifically and are currentTarget and animator already defined in the code?

Line 21 states : animator.SetBool(“isAttacking”, false);

Here is the entire Attacker.CS Code

using UnityEngine;
using System.Collections;

[RequireComponent (typeof(Rigidbody2D))]
public class Attacker : MonoBehaviour {

	private float currentSpeed;
	private GameObject currentTarget;
	private Animator animator;
	
	void start(){
		animator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	 void Update () {
		transform.Translate (Vector3.left * currentSpeed * Time.deltaTime);
		if (!currentTarget) {
			animator.SetBool("isAttacking", false);
		} 	
	}
	
	void OnTriggerEnter2D () {
	}
	public void SetSpeed (float speed) {
		currentSpeed = speed;
	}
	//Called from the animator at the time of actual blow
	public void StrikeCurrentTarget (float damage) {
		if (currentTarget){
			Health health = currentTarget.GetComponent<Health>();
			if (health){
				health.DealDamage (damage);
			
			}
		}
	}
	
	public void Attack (GameObject obj) {
		currentTarget = obj;
	}
}

Do all attackers have an animator on them would be the first thing to check. The best way to flag this is to add a debugging statement such as:

if (!currentTarget) {
    if(!animator) Debug.Log(name+" has no animator attached");
    animator.SetBool("isAttacking", false);
} 	

If that debug statement never appears when playing the game then the only other possible issue would be that the “isAttacking” is incorrect in some way so either it’s not in the animation of all attackers or it’s spelled incorrectly.

First, thank you for taking the time to help, I truly appreciate it.

When I added your debug code to the attacker script it did generate a no animator attached message for both the lizard and the fox, yet the inspector is showing an animator. What do I need to do to fix this?

The first thing I would look at is how were these two attackers generated? Were they pre-placed on the scene or were they instantiated by code? If they were instantiated then the likely issue is that your prefab doesn’t have the animator while the on scene versions do have. If not then I’d try removing and re-adding the animator component to them.

I have tried it both ways. I have tried placing the attackers into the scene and I have cloned attackers that are being spawned. I have checked that they both have animators on them. Since reading your last response , I have tried removing the animators and replacing them. I made sure to place the lizard and fox controllers back on them as well. I am still getting the same no animator attached message.

Can you zip the project and link it here. I’ll download it when I get home and have a look at it to see if I notice anything out of place.

Martyn,
I apologize for taking a few days to get back with you. We had a family emergency.
Here is the link to the project zip file. I worked up through lecture 172 ( Using Stars As Currency) hoping that there would be a clue in a subsequent video that would help me solve the problem, but I just kept getting more glitches.

https://www.dropbox.com/s/lk1p0oogzn43f1r/Glitch%20Garden.zip?dl=0

Hi,

In your Attacker.cs script you Start method is spelt with a lowercase “s”, thus start, as such Unity ignores it.

The problem for you here is that is where you are initialising your animator.

void start(){
	animator = GetComponent<Animator>();
}

Correct the name of the Start method and that error should go away :slight_smile:

void Start(){
	animator = GetComponent<Animator>();
}

Do the same in these too;

  • DefenderSpawner.cs
  • Shooter.cs

I can’t believe I missed that!

1 Like

To be fair it is pretty subtle. :slight_smile:

That seemed to fix it. I looked through these scripts several times and never saw that. Thank you both!!!

1 Like

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

Privacy & Terms