object always appears in front of another

This problem is really annoying and I have been at it for a couple days. I have two objects involved with this problem: a non-moving island and a player. I want the player to appear in front of the island. so I put the player in a different sorting layer. But this didn’t seem to work so I tried multiple methods, including fiddling with the Z-Axis of the player, and I even changed their order within the layer, and watched the Sorting Layer tutorial. Nothing seems to be working; the island stays in front of the player! Here are the sprite renderers of the two objects, the first being the player and the second being the island.
118308-problem-4.jpg
118307-problem-3.jpg
I’ll comment a picture of both objects below the question. Thanks!

Here is a picture of both. [118309-problem-2.jpg|118309]

You are right. I have no idea why GetComponentsInChildren did not work for my case before. Now it works completely fine.

4 Answers

4

Your Player just needs a higher order in the layer!
For example:
Green: default layer, order in layer -1,
Blue: default layer, 0,
and Pink: default layer, 1.
All the visible objects have the same layer but with a different order.

118313-layers.png

for more information check this out: https://docs.unity3d.com/Manual/Layers.html AND https://answers.unity.com/questions/726726/which-is-the-difference-between-layer-and-sorting.html

So I have already tried this method and I tried it again, still the player shows up behind the island. I set the player's order in the layer to 1 and the island's to -1. Both are in the default layer.

I am pretty sure this did not work before. An external library I was using had a similar method implemented and removed it in its newest version. So I was not the only one with that problem. But the problem seems to be fixed by Unity now.

Go to layers on the top right corner → Hit Edit Layers.
You’ll see tags and sorting layers. You can drag each sorting layer up and down using the handle on the left.
The higher the layer is in that list , the deeper it does into the screen.

I have already done this for my layers and it doesn't seem to be working as well.

Hmm. This is weird.. Try.. 1.Having them in the same sorting layer and changing the z value. 2.Going into 3D Mode and manually panning them by selecting the gameObject and pressing w. 3.Create a background sorting layer , pull it to the top , and set your islands sorting layer to background. 4.If nothing works.. I think you found a bug.

No, I work with Unity for over 10 years now and GetComponentsInChildren existed for all that time and always worked that way. GetComponentInParent was added later but exists for about 5 years I think, so nothing new for sure. The only change that was added to GetComponentsInChildren was the optional includeInactive parameter. Though this als exists for at least 5 years.

Hmm. This is weird… Try…
1.Having them in the same sorting layer and changing the z value.
2.Going into 3D Mode and manually panning them by selecting the gameObject and pressing w.
3.Create a background sorting layer , pull it to the top , and set your islands sorting layer to background.
4.If nothing works… I think you found a bug.

Had a similar problem recently and going into 3D mode sorted it, thank you

Hey guys, I figured it out using a dumb but workable solution.

I just made an empty gameobject, put the same components as the player in it, set it to a higher layer than the island, and it worked! The problem was about the player itself and not about the island. I wish their was a better solution, and I going to do more research into this.