Website powered by

Dynamic Clothing Swap Script for Unity

Article / 18 November 2023


In the realm of game development, enhancing player experience through customization is a key goal. I've developed a custom script for Unity to dynamically swap and rig clothing on characters, a feature not natively available in Unity. This approach is ideal for games where character customization is a focal point, such as RPGs or simulation games. Here's how it works:

  • Model Compatibility: The first step involves importing your character and clothing models into Unity. They should have compatible rig structures to integrate smoothly.
  • Rig Configuration: Set up your character model as a humanoid rig in Unity, ensuring all bones are correctly mapped in the model's 'Rig' tab within the import settings.
  • Custom Scripting: The core of this functionality lies in a custom C# script. This script is designed to instantiate clothing items and align their rigs with the character’s rig, allowing for seamless clothing changes.

Here’s a snippet of the script:

using UnityEngine;
public class ClothesSwapper : MonoBehaviour
{
    public GameObject avatarPrefab; // Prefab of the avatar
    public GameObject[] hairOptions; // Array of hair options
    public GameObject[] outfitTops; // Array of outfit tops
    public GameObject[] outfitBottoms; // Array of outfit bottoms
    public GameObject[] footwears; // Array of footwear options
    public GameObject[] glassesOptions; // Array of glasses options
    private GameObject currentAvatarInstance; // Reference to the current avatar instance
    private GameObject[] currentClothingItems; // Array to keep track of the current clothing items
    void Start()
    {
        RandomizeOutfit(); // Randomize outfit on start
    }
    GameObject GetRandomItem(GameObject[] items)
    {
        if (items == null || items.Length == 0)
        {
            Debug.LogError("Items array is empty or null.");
            return null;
        }
        int randomIndex = Random.Range(0, items.Length);
        return items[randomIndex];
    }
    GameObject InstantiateClothingItem(GameObject itemPrefab, Transform parent)
    {
        if (itemPrefab == null) return null;
        GameObject itemInstance = Instantiate(itemPrefab, parent);
        itemInstance.transform.localPosition = Vector3.zero;
        itemInstance.transform.localRotation = Quaternion.identity;
        itemInstance.transform.localScale = Vector3.one;
        // Search for mesh object in the file
        MeshRenderer itemMeshRenderer = itemInstance.GetComponentInChildren();
        if (itemMeshRenderer != null)
        {
            // Additional logic to handle mesh objects can be added here
        }
        else
        {
            Debug.LogError("No MeshRenderer found on the instantiated item.");
        }
        return itemInstance; // Return the instantiated item
    }
    public void RandomizeOutfit()
    {
        if (avatarPrefab == null)
        {
            Debug.LogError("Avatar prefab not assigned!");
            return;
        }
        // Destroy the current avatar and its clothing if they exist
        if (currentAvatarInstance != null)
        {
            // Remove existing clothing items
            if (currentClothingItems != null)
            {
                foreach (var item in currentClothingItems)
                {
                    if (item != null)
                    {
                        Destroy(item);
                    }
                }
            }
            Destroy(currentAvatarInstance);
        }
        // Instantiate a new avatar
        currentAvatarInstance = Instantiate(avatarPrefab, transform.position, Quaternion.identity);
        // Instantiate new clothing items and keep track of them
        currentClothingItems = new GameObject[]
        {
            InstantiateClothingItem(GetRandomItem(hairOptions), currentAvatarInstance.transform),
            InstantiateClothingItem(GetRandomItem(outfitTops), currentAvatarInstance.transform),
            InstantiateClothingItem(GetRandomItem(outfitBottoms), currentAvatarInstance.transform),
            InstantiateClothingItem(GetRandomItem(footwears), currentAvatarInstance.transform),
            InstantiateClothingItem(GetRandomItem(glassesOptions), currentAvatarInstance.transform) // Include glasses in the outfit
        };
    }
}


  • 4. Testing and Refinement: After implementing the script, rigorous testing with various clothing items and animations is essential. This helps identify and rectify any errors or unexpected behaviors.


This script was created as an experiment to push the boundaries of character customization in Unity. It showcases how a bit of creativity and coding can significantly enhance the interactive elements of a game, offering a more personalized and immersive player experience.

Note: The character models downloaded from www.readyplayer.me