Please refer the below image for understanding. I have three elements - background(layer 0), foreground(layer 1) and the object. I am working on 2D game. I have used sprite renderer to set background and foreground images. I am instantiating the object during runtime. How do i make Part-A of the object invisible? I want to make object visible only on foreground…If the object is completely on background, it should be completely invisible. if the object is completely on foreground, it should be completely visible. Part-A and Part-B are just for reference. It is one single object.

Ozan_2
2
@knarola Hi. Under “Sprite Renderer” component, there is an integer value named “Order in Layer”. In this case, you have to set ForeGround’s OrderInLayer as 3, Object’s layer as 2 and BackGround’s layer as 1. (Bsaically, higher layer will be rendered on top)
There is another solution named “Sorting Layer” which is also under Sprite Renderer component. You can create some Layers from the Inspector and change an object’s SortingLayer with the layers that you created. You can find detailed documentation here: SpriteRenderer
EDIT: BackGround sprite have to be fullscreen image. Resize it to fit the camera and set its order in layer as 0. ForeGround image have to be smaller than background image (like half of it). Resize it and set its order in layer as 2. Finally, your object’s order in layer must be 1. It will look like that:
Ozan_2 : My Object is on highest layer. So sorting layer is not helping when part of it lies on foreground and other part in background. It’s like background covering the whole screen is on layer 0, then foregound is placed above that on layer 1 and finally the object instantiates on layer 2 above the foreground.
You are completely right with what you have suggested. But i actually want the other way round as shown in the image.