-
For loop to count layers not behaving as it should
Hi folks,
I’m working on a project and I need to count the number of layers with a specific name and display it as a number on screen. I’ve been using a for loop but unfortunately, I can’t quite get it right and I have the feeling I’m missing a condition in the code but I can’t figure out what it is.
When I apply the following code to a text layer, it displays the correct number when I add or delete a layer called Depth provided it happens below the text layer. But if I add or delete layers above the text layer, the number increases or decreases the number, regardless of what the layer is called.
I know it’s probably something obvious but I’m stumped. Can anyone help please?
var result = 0;
for (var i = 1; i <= thisComp.numLayers; i++) {
var compLayer = thisComp.layer(i);
var layerName = compLayer.name;
if (layerName == "Depth") {
result+=i;
break;
}
}
result;