Skip to content
The Chat Archive
gpt-4o-mini-2024-07-18· OpenAI WildChat-4.8M (AI2)

Write me a C# script for Unity that will populate a list full of BaseBones from a…

  1. User

    Write me a C# script for Unity that will populate a list full of BaseBones from a predefined gameobject named Base then populate a list full of OtherBones from a predefined gameobject named Other and for each bone in both lists if OtherBones matches a bone name in BaseBones then parent the OtherBones bone to the BaseBones bone and match the OtherBones position, rotation with the BaseBones bone.

  2. Assistant

    Certainly! Below is a C# script for Unity that fulfills your requirements. This script assumes that there are two predefined GameObjects in your scene named "Base" and "Other". It will populate two lists of Transform objects representing the bones, and for each matching bone in OtherBones, it will parent it to the corresponding BaseBones bone and match its position and rotation.

    You can create a new C# script in Unity, paste the following code into it, and attach the script to any GameObject in your scene (or create an empty GameObject specifically for this purpose).

    using UnityEngine;
    using System.Collections.Generic;
    
    public class BoneParenting : MonoBehaviour
    {
        public GameObject baseObject; // Assign in inspector or find by name
        public GameObject otherObject; // Assign in inspector or find by name
    
        private List<Transform> baseBones = new List<Transform>();
        private List<Transform> otherBones = new List<Transform>();
    
        void Start()
        {
            // Find the base and other objects if they are not assigned
            if (baseObject == null)
                baseObject = GameObject.Find("Base");
            if (otherObject == null)
                otherObject = GameObject.Find("Other");
    
            // Get all child bones from Base and Other
            PopulateBoneLists();
    
            // Parent matching OtherBones to BaseBones
            ParentMatchingBones();
        }
    
        private void PopulateBoneLists()
        {
            // Clear the lists to avoid duplicates
            baseBones.Clear();
            otherBones.Clear();
    
            // Populate baseBones list
            foreach (Transform bone in baseObject.transform)
            {
                baseBones.Add(bone);
            }
    
            // Populate otherBones list
            foreach (Transform bone in otherObject.transform)
            {
                otherBones.Add(bone);
            }
        }
    
        private void ParentMatchingBones()
        {
            foreach (Transform otherBone in otherBones)
            {
                // Check if there is a matching base bone
                Transform matchingBaseBone = baseBones.Find(baseBone => baseBone.name == otherBone.name);
                
                if (matchingBaseBone != null)
                {
                    // Parent OtherBone to BaseBone
                    otherBone.SetParent(matchingBaseBone);
    
                    // Match position and rotation
                    otherBone.localPosition = Vector3.zero; // Reset local position
                    otherBone.localRotation = Quaternion.identity; // Reset local rotation
                }
            }
        }
    }
    

    Instructions to Use:

    1. Create a new script in your Unity project and name it BoneParenting.
    2. Copy and paste the code above into the script.
    3. Attach the script to an empty GameObject in your scene.
    4. In the Inspector, you can set the Base and Other references manually or let the script find them automatically.
    5. Make sure that the Base and Other GameObjects in your scene have child GameObjects named the same way as the bones you wish to match.
    6. Play the scene, and the script will automatically execute the parenting and position matching based on the bone names.

    Note:

    • This script assumes that the bones are structured as children of Base and Other GameObjects.
    • If bones are organized differently, you may need to adjust how the lists are populated.