Interaction Script

I have two scripts, one is PlayerInteraction and the other is Interactable. Right now they’re what’s going to be handling the focus on interactable objects.
PlayerInteraction is attached to the player and handles what I click and calls on some methods from Interactable. Interactable is attached to the interactable object and has some defines some methods for the player to access and to add in selection cursors to the object later on (I’ve left out that part of the code).

Right now the issue is for a reason that I’m not catching (new to this), the variable isFocus, which would be used to trigger things like cursors later on, turns true when you click an object, however immediately sets itself to false afterwards. I can’t seem to find out why. isFocus is in both OnFocused() and OnDefocused() methods, which are called on by the player when an object with the interactable script is clicked on screen.

PlayerInteraction.cs:

using UnityEngine;
using System.Collections;

public class PlayerInteraction : MonoBehaviour {
    public GameObject mainCameraObject;
    private Camera cam;
    public Interactable focus = null;
    Transform player;

    void Start() {
        cam = mainCameraObject.GetComponent<Camera>();
    }

    void Update() {
        if (Input.GetMouseButtonDown(0)) {
            Ray ray = cam.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit)) {
                Interactable interactable = hit.collider.GetComponent<Interactable>();

                if (interactable != null) {
                    SetFocus(interactable);
                   
                } else RemoveFocus();
            }
        }
    }

    void SetFocus (Interactable newFocus) {
        if (newFocus != focus) {
            if (focus != null) {
                focus.OnDefocused();
            }
            focus = newFocus;
        }
       
        newFocus.OnFocused(transform);
    }

    void RemoveFocus() {
        if (focus != null){
            focus.OnDefocused();
        }
        focus = null;
       
    }
}

Interactable.cs:

using UnityEngine;

public class Interactable : MonoBehaviour
{
    public float radius = 3f;
    public float maxSelectDistance = 25f;
    bool isFocus = false;
    Transform player;
    bool hasInteracted = false;
    public GameObject cursorImage;
    public GameObject mainCameraObject;
    public Renderer selectedObject;
    Camera cam;
    Vector3 heightOffsetPos;
    Vector3 screenPos;
    Vector3 twoDimPos;

    void Start() {
        isFocus = false;
        player = GameObject.Find("MouseKnight").transform;
        cursorImage = GameObject.FindWithTag("Select Cursor");
        cursorImage.SetActive(true);
        cam = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
        //selected object should be whatever the player has focused.
    }

   

    public virtual void Interact() {
    }
    void Update() {
        //used to test the status of isFocus
        if (isFocus) {
            print(player.GetComponent<PlayerInteraction>().focus.name);
        }
        //used for tracking object's position
        selectedObject = GetComponent<Renderer>();

        initInteract();
        //placeCursor();
    }

    //checks if we're close enough to interact & if we've already interacted
    public void initInteract() {
        if (isFocus && !hasInteracted) {
            float distance = Vector3.Distance(player.transform.position, transform.position);
            if (distance <= radius) {
                Interact();
            }
        }
        hasInteracted = true;
    }

    //turns on isFocus and is used in the PlayerInteraction Script
    public void OnFocused (Transform playerTransform) {
        isFocus = true;
        print("isFocus : " + isFocus);
        player = playerTransform;
        hasInteracted = false;

    }

    //turns off isFocus and is used in the PlayerInteraction Script
    public void OnDefocused() {
        isFocus = false;
        print("isFocus : "+ isFocus);
        player = null;
        hasInteracted = false;
    }
}

Any help would be appreciated. I’m sure it’s something simple I’m missing but yeah, thanks for taking the time.

Your logic seems fine. Throw some log statements in your SetFocus and RemoveFocus functions and see if they’re being called at odd times as they’re the only location you call OnDefocused from.