I have setup a get/set bool in my FieldOfView class and in my fox class i try and do FieldOfView.CanSeePlayer but it always returns an error when I start the game it gives me a null reference error. The error is from the fox script on the Debug.Log(fov.CanSeePlayer);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fox : MonoBehaviour
{
private Vector3 _horizontalMovement;
public int direction;
Animator animator;
public Transform Player;
public Rigidbody2D rb;
SpriteRenderer spriteRenderer;
public FieldOfView fov;
public float moveSpeed = .7f;
// FieldOfView fov = new FieldOfView();
void Start(){
direction = 0;
animator = GetComponent<Animator>();
rb = this.GetComponent<Rigidbody2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update(){
Vector2 dir = Player.position - transform.position;
//error here
Debug.Log("[Can see] "+ fov.CanSeePlayer);
// if(fov.CanSeePlayer == true){
// rb.MovePosition(rb.position + dir * moveSpeed * Time.fixedDeltaTime);
// }
}
public enum orient{
left,
right,
up,
down
}
}
Inside my field of view class i setup the CanSeePlayer bool
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FieldOfView : MonoBehaviour
{
// Start is called before the first frame update
public float radius = 1f;
//angle enemy can see
[Range(1, 360)]public float angle = 90f;
public LayerMask targetLayer;
public LayerMask obstructionLayer;
public GameObject player;
public bool CanSeePlayer{get; private set;}
void Start()
{
CanSeePlayer = false;
player = GameObject.FindGameObjectWithTag("Player");
StartCoroutine(FOVCheck());
}
private IEnumerator FOVCheck(){
WaitForSeconds wait = new WaitForSeconds(0.2f);
while(true){
yield return wait;
FOV();
}
}
private void FOV(){
Collider2D[] rangeCheck = Physics2D.OverlapCircleAll(transform.position, radius, targetLayer);
if(rangeCheck.Length > 0){
Transform target = rangeCheck[0].transform;
Vector2 directionToTarget = (target.position - transform.position).normalized;
if(Vector2.Angle(transform.up, directionToTarget) < angle / 2){
float distanceToTarget = Vector2.Distance(transform.position, target.position);
if(!Physics2D.Raycast(transform.position,directionToTarget, distanceToTarget, obstructionLayer))
CanSeePlayer = true;
else
CanSeePlayer = false;
}
else
CanSeePlayer = false;
}
else if(CanSeePlayer)
CanSeePlayer = false;
}
private void OnDrawGizmos(){
Gizmos.color = Color.white;
UnityEditor.Handles.DrawWireDisc(transform.position, Vector3.forward, radius);
Vector3 angle01 = DirectionFromAngle(-transform.eulerAngles.z, -angle / 2);
Vector3 angle02 = DirectionFromAngle(-transform.eulerAngles.z, angle / 2);
Gizmos.color = Color.yellow;
Gizmos.DrawLine(transform.position, transform.position + angle01 * radius);
Gizmos.DrawLine(transform.position, transform.position + angle02 * radius);
if(CanSeePlayer){
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, player.transform.position);
}
}
private Vector2 DirectionFromAngle(float eulerY, float angleInDegrees){
angleInDegrees += eulerY;
return new Vector2(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
}
}