title: Unity 2D: Sorting layer changes when shader changes
body: <p>I have a simple script that detects the nearest interactable and highlights it by applying a shader with an outline. For some reason when the shader is applied it changes layer order and goes under the items above it. In both situations the sorting layer is set to 0 (default). How can I troubleshoot this situation further?</p>
  <p><strong>Example</strong></p>
  <p><em>Sprite over</em></p>
  <p><a href="https://i.stack.imgur.com/cHedG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cHedG.png" alt="Sprite over" /></a></p>
  <p><em>Sprite under</em></p>
  <p><a href="https://i.stack.imgur.com/TJGRJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TJGRJ.png" alt="Sprite under" /></a></p>
  <p><strong>Code</strong></p>
  <pre><code>using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class CheckInteractable : MonoBehaviour
  {
      [SerializeField] private float range;
      [SerializeField] private Material outlineMat;
      private Material originalMat = null;
      private GameObject closestObject = null;
      void Update()
      {
          Collider2D[] colliderArray = new Collider2D[5];
          colliderArray = Physics2D.OverlapCircleAll(transform.position, range, LayerMask.GetMask(&quot;Interactable&quot;));
          
          if(colliderArray.Length &gt; 0)
          {
              if (colliderArray.Length == 1)
              {
                  if(closestObject != colliderArray[0].gameObject)
                  {                    
                      makeClosest(colliderArray[0].gameObject);
                  }
                      
              }                
              else
              {
                  foreach (Collider2D collider2D in colliderArray)
                  {
                      float dist = Vector2.Distance(transform.position, collider2D.transform.position);
                      float closestDist;
  
                      if (closestObject != null)
                          closestDist = Vector2.Distance(transform.position, closestObject.transform.position);
                      else
                          closestDist = Mathf.Infinity;
  
                      if (dist &lt; closestDist)
                          makeClosest(collider2D.gameObject);
  
                  }
              }
  
              print(&quot;closest: &quot; + closestObject.name + &quot;\tDist: &quot; + Vector2.Distance(transform.position, closestObject.transform.position));
          }      
          else if(closestObject != null) 
          {
              closestObject.GetComponent&lt;SpriteRenderer&gt;().material = originalMat;
              originalMat = null;
              closestObject = null;
          }
          
      }
  
      void makeClosest(GameObject obj)
      {
          if (closestObject != null)
              closestObject.GetComponent&lt;SpriteRenderer&gt;().material = originalMat;
  
          originalMat = obj.GetComponent&lt;SpriteRenderer&gt;().material;        
          obj.GetComponent&lt;SpriteRenderer&gt;().material = outlineMat;
          closestObject = obj;
      }
  }
  </code></pre>