Raycasting - when looking away from a collider object UI disables?

THE LANGUAGE I AM USING IS C#!

Hello there, so I have been messing around with my script for a while now and I am trying to get it so when i look away from a collider object that the UI is disabled. I am using simple UI canvas and SetActive statements.

But I’m not sure where I can place the code or how I’d write the code to disable the UI when you look away from the item that is tagged “FoodItem” and “WaterItem”.

Here is the code I Use when looking at the item to enable the UI, but when I look away from the item the canvas is still on my screen…

        if (Physics.Raycast(ray, out hitinfo, interactRange))
        {
            if (canHover = true)
            {
                Debug.Log("Tesing");
                if (hitinfo.collider.tag == "FoodItem")
                {
                    eatFood.SetActive(true);
                    drinkWater.SetActive(false);
                    Debug.Log("Looking at food!");
                }
                if (hitinfo.collider.tag == "WaterItem")
                {
                    eatFood.SetActive(false);
                    drinkWater.SetActive(true);
                    Debug.Log("Looking at water!");
                }
            }
        }
        }

This script is using variables from another script also. can anyone help me to be able to disable the two canvases when not looking at them please.

Thanks guys <3

You are not making much sense. Are you sure you want to enable the UI when you look away from it?

Sorry man I typed that out lastnight have 14 hours of programming yesterday and I was falling asleep :confused: when you look away from the item I want to be able to disable the UI, i.e. Make it false.

also I updated the post!

if(hit food){
    show food UI;
} else if(hit water){
    show water UI;
} else {
    hide all UI;
}
1 Like

Okay I will try that now!

I tried this and it didnt work, I have to look at a different object that does have a tag or else it will still stay on screen. I need something to check if nothing is in front of you then disable the UI

NEVER MIND PEOPLE SOLVED IT MYSELF!