Your comments

Good catch on noticing that exclamation point. Yes, it does appear to be intentional, because I see the same exclamation point in the 2018.1 version which I definitely know was working correctly. You are correct though, and it's making my head hurt just looking at it. The comments seem to suggest it's skipping background pixels, but the code looks like it is skipping everything BUT background pixels. It could very well be that my comments were from a really old version and I never changed the comments... so the comment could be misleading possibly. But the exclamation point is in every version, so it's definitely intentional.

I see that it calls the ShadowTextureLoop() twice, and the second time, it passes the colors of the tree again, so it's not "deleting pixels".. it calls the function again and passes the colors of the tree a second time. So it's doing it in two passes. Anyway, so perhaps the opposite was true, and there are actually NO background pixels in your screenshot? So maybe your tree is somehow filling every single pixel in the screenshot so that it can't find any background pixels? 

When it's actually taking the screenshots, you should be able to very briefly see the tree, and it'll turn the tree to several different angles, as it is taking the screenshots. The tree should basically fit the view perfectly. it resizes the tree (or the camera, I can't remember) so that it fits... I think I see that resizing code on line 713. So the tree should fit the view perfectly. So it's unusual if it can't find even a single background pixel. The tree would have to be perfectly square or something. When you click the Play button to start the screenshotting, do you see like a huge tree that fills the whole view? Or is it somehow possible that you are running some kind of full screen effect that is altering the color of your camera's background, so that the background behind the tree's isn't completely black? Also... make sure that the tree doesn't somehow have a billboard attached to it... as that billboard could actually BE square, and could potentially fill the whole view, which might explain if it can't see the background in the back at all.

You say screenshotpic_nobark is full of valid colors... but are they all the same color..? the background color? or is it full of different colors, such as the colors of a tree? I'm guessing it's an arroy full of nothing but the background color. And if so, then the problem must be back on line 777 to 822, where it takes the screenshot of specific parts of the tree, I think it's turning the entire tree transparent and taking a screenshot of nothing but background pixels. If you can step through the lines at 777 through 822 you can find out if it is successfully turning "on" the leaves, or if it's failing to find any leaves and simply turning the entire tree transparent.

Oh, okay that might work. I was worried you might have been using notepad or something.

Oh I forgot to say, during the install process for visual studio, you probably will see an option to install "tools for Unity", you might need to make sure it installs that also, in case that's important, probably is.

Oh... ack... you might not even be using Visual Studio to debug. Are you using a windows computer? If you are using windows, you can get it for free from

You'll need to install it, and make sure it installs C# for you. Once that install is done, you'll need to restart unity probably, and then in the unity editor, go to Edit->Preferences->External Tools-> and set the external script editor to Visual Studio. Once that's done, you simply double click a script in your project window, and it should open it in visual studio for you. Then you can go to line 777 or whatever, click the line, and press F9 to set a break point. Then.. uh... click the green arrow at the top to "attach to Unity", which hopefully it attaches with no errors. Once it says it is ready, then run the scene like normal and it should stop it when it hits that line, and you can then hover your mouse over each variable to see what their exact values are. And then you can tell it to continue one line at a time (there's a button along the top to tell it to go forward one line at a time), so you can see where it goes next and what it's doing.

But if you are using an Apple Mac computer then you might be out of luck.

In that case, I would put a breakpoint on line 777 and check to see if it successfully finds both leaves and bark in your tree. Because only thing I can think of that would create that divide by zero error is if it isn't find one or the other. Either that... or... somehow your tree is less than a pixel in size, or it has leaves but they are all so tiny it can't find them in the screenshot it is taking... or that somehow BillboardTextureSize is set to zero. But best bet is that it isn't finding either the leaves or the bark, which it screenshots between lines 777 and 822. It then transfers that screenshot at line 1060, which takes it to line 2209, which is where the actual divide by zero occurs.

Yes it generates them. So what's the exact name of the "leaf" shader? It's the name of the shader that matters, not the name of the auto-generated material. You'll need to find the material of the leaves, and look at the shader name. It needs to have the word "Leaves" in it. For Unity 2017.1, it really should have the word "Leaves" in the shader name.

Or then again, it also can occur if your entire tree is using shaders that do NOT have the word "Leaves" anywhere in the shader names. Looks like my code expects your tree to be using Unity Tree Creator shaders, which have the word "Leaves" and the word "Bark" in the names of the shaders. If you are somehow using a tree that has shaders that don't have these words in the shader names, that could cause this error from the looks of it.

Looks like this error can occur if your entire tree is using a "Bark" shader. You might check the leaves of your tree and see every single material on your tree is using Unity's "Bark" shader.

It tries to screenshot the leaves seperately from the bark, by temporarily setting the bark to fully transparent. This happens on line 797. If your whole tree is using a shader with the name "Bark", then the whole tree will disappear for that particular screenshot. And then, it finds no pixels, so it divides by the number of pixels found, which would be zero, so it's a divide by zero error. Just check your tree if it is using nothing but bark shaders, and if so, see if you can switch the leaves of the tree over to using a shader that doesn't have "Bark" in the name.

If that's not what's causing the error, you might just need to put a break point on line 793 and step through it one line at a time to see if it is finding both bark and something that isn't bark, because it ought to have part of the tree using bark shader, and part of the tree using leaves shader.

I did a pretty significant update to the trees on Unity 2018.1, which added the ability for the shadows in the trees to properly handle ambient light colors. In that same update, I fixed some issues with ambient occlusion, and put a lot of additional options into the manager to control details about the size, shape, and sharpness of the shadows in the leaves. It's quite possible also that this particular bug might have been fixed while I was at it. Because I haven't gotten any reports of bugs this severe in over 6 months.

I'll check out this error on the 2017.1 version for you, because I did want to maintain support for the older versions, but if you are able to upgrade to 2018.1 without wrecking your project, you might like some of the features and bug fixes that were in that big update.