- Home /
,Help Toggling a Boolean Value via a Raycast Using Multiple Raycasts (Light-Detection System)
So I'm trying to create a system that detects whether or not a player is within the light-range of any light in my scene; essentially a script that will allow me to decrease a pool of Sanity points whenever the player is in the dark.
Here is the script I have attached to each of the lights in my scene:
This script simply LooksAt my player at all times, shoots a ray towards the player (with a max range of whatever the light-range is of the light it is attached to), and if that ray actually hits the player, it will set the boolean "inLight" (that is on a script on my player) equal to true; else it will set "inLight" to false.
Here is the script I have attached to my player for context:
So, this system actually works... if there is only one light in my scene. The problem is, is that if my player is standing in a light (thus making "inLight" = true) and then exits that light (thus making "inLight" = false), "inLight" will continue to stay false upon entering a different light because the else-statement from the previous light is still setting "inLight" to false despite a duplicate of that script trying to then set "inLight" to true.
I hope I didn't make this post too confusing, but I tried to explain it best I can. I have tried deleting the else-statement within my LightCast script, and in-turn adding " inLight = false; " inside the Update function in my SanitySystem script, but that unfortunately gives the same result.
Your answer
![](https://koobas.hobune.stream/wayback/20220613004408im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Bool works everywhere but specific if statement. 1 Answer
I'm having trouble setting a bool. 2 Answers
Check a boolean function without using Update() 1 Answer
If statement always true 3 Answers
Why won't my if result to true? 1 Answer