Fox Not Attacking

Not sure if people are still reading these, but will give it a shot. After adding the health component, my fox will no longer attack defenders. He’ll jump over stones, but not go into attack mode. The issue appears to be with this bit of code:

	if (!currentTarget) {
		animator.SetBool("isAttacking", false);
	}

If I comment that out, he works just fine. So apparently he’s dropping his current target immediately and just continuing to walk. I can’t for the life of me figure this one out since the code appears to be exactly the same as what’s in the lesson.

Any help would be appreciated. The full code of my attacker and fox scripts is below.

using UnityEngine;
using System.Collections;

public class Attacker : MonoBehaviour {

	private float currentSpeed;
	private GameObject currentTarget;
	private Animator animator;
	
	void Start () {
		animator = GetComponent<Animator>();
	}
	
	void Update () {
		transform.Translate (Vector3.left * currentSpeed * Time.deltaTime);
		if (!currentTarget) {
			animator.SetBool("isAttacking", false);
		}
	}
	
	public void SetSpeed(float speed){
		currentSpeed = speed;
	}
	
	public void StrikeCurrentTarget(float damage){
		Debug.Log (name + " caused damage " + damage);
		if (currentTarget) {
			Health health = currentTarget.GetComponent<Health>();
			if (health) {
				health.ReduceHealth (damage);
			}
		}
	}
	
	public void Attack(GameObject obj){
		currentTarget = obj;
	}
}

using UnityEngine;
using System.Collections;

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

	private Animator animator;
	private Attacker attacker;

	void Start () {
		animator = GetComponent<Animator>();
		attacker = GameObject.FindObjectOfType<Attacker>();
	}
	
	void OnTriggerEnter2D (Collider2D col){
		GameObject obj = col.gameObject;
		if (!obj.GetComponent<Defender>()) {
			return;
		} else if (obj.GetComponent<Stone>()) {
			animator.SetTrigger("jumpTrigger");
		} else {
			Debug.Log ("Fox triggered attack");
			animator.SetBool ("isAttacking", true);
			Debug.Log ("Fox should be attacking.");
			attacker.Attack(obj);
		}
	}	
}

I was running into weird issues with the script no working correctly and I couldn’t figure out what the problem was no matter how hard I looked. I just closed Unity and the Mono Develop all together and then start Unity again. The problem then went away.

I think the issue might be the link between Mono Develop and Unity got broken somehow and the code is not working like it’s supposed to. Try adding public float viewValue; or something like that to your Attacker class and see if the script in Unity actually updates with the new code.

I think the issue might be with the access to object that caused the script to corrupt after a while is the

StrikeCurrentTarget (float damage)

When we call

Health health = currentTarget.GetComponent<Health>();

we expect that the currentTarget variable to actually be there but this is not always true since the currentTarget variable never get sets until we call the

Attack(GameObject obj) function.

That’s the only time when currentTarget is set to the object that the attacker sees. I think this could really cause an unexpected bug in general when we try to use an object without ever setting it.

Not sure whether Ben is addressing this problem on the next section or not since I just finished up with this video also. I think a better way to set the current target would be to assign it upon OnTriggerEnter2D since that’s when you know for sure that you ran into something. Also it’s weird that we have OnTrigger2D and Update() on both the Attacker.cs and Fox.cs since we shouldn’t use OnTrigger2D on the Attacker and on Fox classes at the same time. It seems like something we should just abstract and require an override at Attacker class and then inherit it down so that we always override it at the sub class (Fox, Lizard, etc.).

It’s confusing who should be handling the animator since both Attacker and Fox are attached to the same object. I guess this is when clarifying the Object hierarchy would make sense. I wonder whether it would be better to use Abstract Class for the Attacker so that it’s more of the parent hierarchy contract of what the Attacker class should implement. I feel that [requireComponent] seems like a non-object way to handle class hierarchy correctly.

Anyway, I don’t know exactly whether your problem came from the code or Unity code execution itself. Might have to keep watching the videos. For now, my problem was fixed with restarting Unity and MonoDevelop.

Thanks for the response! I came back to this after a little break since work was kicking my ass. The issue appears to be with multiple attackers. I can load any single attacker and it behaves properly, but once I add another attacker of any kind only one will stop and attack. I’ll work on narrowing it down further to figure it out. I did try restarting everything, just in case. No love there.

do you get any of those Debug.Log() popping out?

e.g.

Debug.Log ("Fox triggered attack");
Debug.Log ("Fox should be attacking.");

Apologies, been more than a year since I done this course, in some ways not really sure where to start with debugging this issue you’re having…

Edited** actually, sounds like you resolved it, but now it occurs when there are multiple attackers? Potentially need to have a look at your whole project because that shouldn’t happen…

1 Like