Enabling an outline around an object when hovering over it or when clicking it is a common use case. Below you will find some example code that shows how you can implement this.

Prerequisites

Before using the script, there are a few prerequisites before being able to interact with the scene. Check the list below if you meet every requirement.

Checklist

1. Event System Make sure there is an EventSystem in your scene. If not, Create > UI > Event System. 2. Physics Raycaster Make sure that your camera has a Physics Raycaster component added to it.

Steps

  1. Add the script to any object that you want to outline. (! object should have a collider !)

  1. Select the Outline Layer in the inspector to be the same as the Layer that you have configured for the outline.

Script

The core functionality of the script below is changing the rendering layer of the selected object’s Mesh Renderer to be the one that the outline is activated for.

Using the Activate option, you can switch between activating the outline On Hover or On Click.

using UnityEngine;  
using UnityEngine.EventSystems;  
// using Linework.Common.Attributes; // unity 2022
  
public class Outline : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler  
{  
    [SerializeField] private RenderingLayerMask outlineLayer; // unity 6
    // [SerializeField] [RenderingLayerMask] private int outlineLayer; // unity 2022
    [SerializeField] private Activate activate = Activate.OnHover;  
  
    private Renderer[] renderers;  
    private uint originalLayer;  
    private bool isOutlineActive;  
  
    private enum Activate  
    {  
        OnHover,  
        OnClick  
    }  
  
    private void Start()  
    {        
	    renderers = TryGetComponent<Renderer>(out var meshRenderer)  
            ? new[] {meshRenderer}  
            : GetComponentsInChildren<Renderer>();  
        originalLayer = renderers[0].renderingLayerMask;  
    }  
    
    public void OnPointerEnter(PointerEventData eventData)  
    {        
	    if (activate != Activate.OnHover) return;  
        SetOutline(true);  
    }  
    
    public void OnPointerExit(PointerEventData eventData)  
    {        
	    if (activate != Activate.OnHover) return;  
        SetOutline(false);  
    }    
    
    public void OnPointerClick(PointerEventData eventData)  
    {        
	    if (activate != Activate.OnClick) return;  
        isOutlineActive = !isOutlineActive;  
        SetOutline(isOutlineActive);  
    }    
    
    private void SetOutline(bool enable)  
    {        
	    foreach (var rend in renderers)  
        {            
		    // unity 6
	        rend.renderingLayerMask = enable 
	        ? originalLayer | outlineLayer 
	        : originalLayer;  

			// unity 2022
			// rend.renderingLayerMask = enable 
	        // ? originalLayer | 1u << outlineLayer - 1 
	        // : originalLayer;  
        }    
	}
}

Issues?

  • check the checklist with requirements above
  • check if a collider may be obscuring your selectable game object
  • if the collider on the selectable object is a trigger, verify that Queries Hit Triggers is enabled in the physics settings
  • check Raycaster Event Mask vs gameobject’s layer mask