Array only detects if last entry in array has a true boolean
I've created a "Light Detection" script (attaches to light objects) where the goal is to shoot a raycast from the light object to the player if the player is within a certain radius. This all works fine.
My problem lies within the secondary "Stealth Management" script (attached to player). The ultimate goal is to check if just ONE light object detects the player, _isPlayerLit = true.
So in order to accomplish this, I figure the script should first create an array, determining how many light objects there are in a scene in the Start function. And then checks every lightObject for the bool value via Update function. If all bool values = false, _isPlayerLit != true.
Simple enough, right?
Well, I'm entirely new to programming, and this is my first run-in with an array. I've spent the past few days trying to understand, but no matter what, the function I create only sets _isPlayerLit to true based on the last light object in the array.
I know it's something stupid-simple, but I just can't seem to find the solution for my problem.
This is my first post on the forum, and I don't really know the exact information you Unity experts need to determine my issue, so I will just give you both of my scripts.
Here's the Light Detection script, which does its job:
{
// lightObject radius variables
public bool playerInRange;
public float lightRange;
Light lightObject;
GameObject player;
// lightObject raycast variables
public bool isPlayerLit;
Vector3 origin;
Vector3 posPlayer;
void Awake()
{
// lightObject gathering info about its variables
player = GameObject.FindGameObjectWithTag("Player");
lightObject = GetComponent<Light>();
lightRange = lightObject.range;
}
void Update()
{
// Checks if player is within range of lightObject
if (Vector3.Distance(lightObject.transform.position, player.transform.position) < lightRange)
{
// If player is in range of lightObject, playerInRange = true & a raycast is cast from the lightObject to the player
playerInRange = true;
RaycastHit hit;
origin = lightObject.transform.position;
posPlayer = player.transform.position;
Debug.DrawLine(origin, posPlayer, Color.red);
// If raycast = true
if (Physics.Linecast(origin, posPlayer, out hit))
{
// If the raycast has no obstructions to the player, isPlayerLit = true
if (hit.collider.gameObject.tag == "Player")
{
isPlayerLit = true;
} else
{
isPlayerLit = false;
}
}
} else
{
// If the player is not in range of lightObject, playerInRange is false, and isPlayerLit can't be true.
playerInRange = false;
isPlayerLit = false;
}
}
}
And here's the Stealth Manager script, where the problem lies:
{
public bool _isPlayerLit;
private GameObject[] lightList;
void Start()
{
lightList = GameObject.FindGameObjectsWithTag("StealthLight");
}
private void Update()
{
for (int i = 0; i < lightList.Length; i++)
{
if (lightList[i].GetComponent<OldLightDetection>().isPlayerLit != true)
{
_isPlayerLit = false;
}
else
{
_isPlayerLit = true;
}
}
}
}
Thank you in advance! ~
Your answer
Follow this Question
Related Questions
How to compress iterative code, perhaps using a list and/or loops? 1 Answer
Generate and translate GameObjects 2 Answers
Dinamically set array in shader 1 Answer
Set variables in an array dependent on which scene the player is in. 1 Answer
index out of range problem with for loops and arrays. 1 Answer