How to get a bool from another class that is get/set

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));
    }
}

Referencing variables, fields, methods (anything non-static) in other script instances:

REMEMBER: it isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

As for NullRef, if that’s all it is, there’s only ONE answer:

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Steps to success:

  • Identify what is null
  • Identify why it is null
  • Fix that

I am trying to have my fox sprite read if he can see the player then run away. I have a generic fov class so in the future I can set different fov’s for different things. I understand that fov.CanSeePlayer is the null value but am unsure why or how to have it so my fox script checks the fov script to see if it can see the player then do something

That’s awesome… but it does not change anything about what I posted above.

If you’re getting a null, fix that first. Three steps. Every minute you wait is a minute it isn’t being fixed.

1 Like

Hey oh thanks for rubber ducky. It was in the UI i didnt connect the boolean to the fox. once i did that i see it working.

What do you see in the Debug.Log? What did you set the fox’s fov to?

Edit: Oh, you fixed it already?