- Home /
Array of objects - script only works on the first object in the array
I'm making a game where my player can hold a torch or lantern. I made a foreach to loop over each object and check if the player is holding that item. But it's only working on the first item of that script.
I made a variable where I get all the objects with this LightController script:
public LightController[] lights;
and this in the start function:
lights = FindObjectsOfType<LightController> ();
In the update function:
foreach (LightController light in lights) {
if (light.holdingLight == true && light.isTorch) {
holdTorch = true;
}
if (light.holdingLight == true && light.isLantern) {
holdLantern = true;
}
if (light.holdingLight != true) {
holdTorch = false;
holdLantern = false;
}
}
Answer by NoseKills · Oct 31, 2017 at 11:25 AM
You loop through every item in the array and set the variables accordingly.
So for example if the last item you check has holdingLight == false
, you set holdTorch
and holdLantern
to false before exiting the loop, no matter if some earlier item had set them to true.
Also if multiple items have their holdingLight as true, you could in theory finish the loop with both holdTorch
and holdLantern
as true.
The code you posted isn't enough to clearly determine if these are the problems that cause what you are seeing, but at least try to address these problems and see if it helps.
Without knowing more, I'm just guessing, but you probably want to set holdTorch
and holdLantern
as false before the loop, not inside it, and exit the loop with a break;
as soon as you find an object that is being held.
I'm not a 100% sure what all your variables mean etc. but it looks like maybe the code could be simplified and some bugs could be avoided by just choosing better suited variables and structure for the code.
If you can only ever hold one light at a time, it's not good to have a holdingLight
bool in each light, as that can result in many lights claiming they are being held. It might be better to have a variable in the object that is holding the light e.g. GameObject lightImHolding
. If it's null, no light is being held etc... That way you have only one variable to keep track of.
Same thing with bools holdLantern and holdTorch. Now they can be both true or false at the same time. If you used for example an enum
instead, you could avoid that problem altogether.
enum LightType {
None,
Torch,
Lantern
}
...
public LightType myLightType;
Now the stored light type can't be 2 things at the same time.
That being said, if this represents the type of the held light, maybe it should be a property of each light so you can check it via lightImHolding
. Otherwise you, again, have multiple variables for one thing and you can have a bug where the light itself and the type variable can conflict if you forget to update them correctly.
Setting both holdTorch and holdLantern to false before the loop solved the problem.
Answer by ItsaMeTuni · Oct 30, 2017 at 11:02 PM
1) Use List instead of an array. 2) Are the lights prefabs? If yes, are you instantiating them before or after this script's start function?