The issue appears to be with the Transform “tgt” on line 54: Could someone explain to me what I’m doing wrong?
using UnityEngine;
using System.Collections;
public class TestEnemyScript : MonoBehaviour {
public GameObject player;
public float speed;
private Transform target;
private float vel;
FieldOfView fov;
CharacterController controller;
Animator animator;
void Start()
{
//store the fov componenet in a variable
fov = GetComponent<FieldOfView>();
//store character controller in variable
controller = GetComponent<CharacterController>();
//store animator in variable
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update ()
{
Anim();
if (fov.visibleTargets != null)
{
foreach(Transform t in fov.visibleTargets)
{
if (t.gameObject.tag == "Player") { target = t;}
}
Move(target);
}
if(animator.GetBool("isDying"))
{
StartCoroutine(Die());
}
}
IEnumerator Die()
{
yield return new WaitForSeconds(5);
animator.SetBool("isDying", false);
Destroy(gameObject);
}
void Move(Transform tgt)
{
if(animator.GetBool("isDying") == false)
{
/*Line 54 */ transform.position = Vector3.MoveTowards(transform.position, new Vector3(tgt.transform.position.x, transform.position.y, tgt.transform.position.z), speed * Time.deltaTime);
transform.LookAt(new Vector3(tgt.transform.position.x, transform.position.y, tgt.transform.position.z));
}
}
void Anim()
{
vel = controller.velocity.magnitude;
Debug.Log(vel);
//isWalking
if (vel != 0)
{
animator.SetBool("isWalking", true);
}
else if (vel == 0)
{
animator.SetBool("isWalking", false);
}
}
}