bubble sort algoritm using list<GameObject>(). input amount for number of gameobjects update the sort based on distance from gameobject to a target gameobject and change color on 50% of the gameobjects closest to the gameobject
Here's an example implementation of the bubble sort algorithm in C# to sort a list of `GameObject` instances based on their distance to a target `GameObject`. We'll also update the color of 50% of the closest game objects. ```csharp using System; using System.Collections.Generic; using UnityEngine; public class GameObjectSorter : MonoBehaviour { // Target GameObject public GameObject target; // List of GameObjects to sort public List<GameObject> gameObjects; // Update is called once per frame void Update() { // Check if the target and game objects are set if (target != null && gameObjects != null) { // Sort game objects based on distance to target gameObjects.Sort((a, b) => (a.transform.position - target.transform.position).sqrMagnitude.CompareTo((b.transform.position - target.transform.position).sqrMagnitude)); // Update color of 50% of the closest game objects int midIndex = gameObjects.Count / 2; for (int i = 0; i < midIndex; i++) { // Randomly select 50% of the closest game objects to change color if (UnityEngine.Random.value < 0.5f) { gameObjects[i].GetComponent<Renderer>().material.color = Color.red; } } } } // Bubble sort algorithm to sort game objects based on distance to target public void SortGameObjects() { int n = gameObjects.Count; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if ((gameObjects[j].transform.position - target.transform.position).sqrMagnitude > (gameObjects[j + 1].transform.position - target.transform.position).sqrMagnitude) { // Swap game objects GameObject temp = gameObjects[j]; gameObjects[j] = gameObjects[j + 1]; gameObjects[j + 1] = temp; } } } } } ``` To use this script, follow these steps: 1. Create a new C# script in your Unity project and name it `GameObjectSorter`. 2. Attach the script to a GameObject in your scene. 3. In the Inspector, assign the target GameObject to the `target` field. 4. Create a list of GameObjects to sort and assign it to the `gameObjects` field. 5. In the `Update()` method, the script will sort the game objects based on their distance to the target and update the color of 50% of the closest game objects. Alternatively, you can use the `SortGameObjects()` method to manually sort the game objects based on their distance to the target. ```csharp public void SortGameObjects() { // Call the bubble sort algorithm SortGameObjectsBubbleSort(); } private void SortGameObjectsBubbleSort() { int n = gameObjects.Count; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if ((gameObjects[j].transform.position - target.transform.position).sqrMagnitude > (gameObjects[j + 1].transform.position - target.transform.position).sqrMagnitude) { // Swap game objects GameObject temp = gameObjects[j]; gameObjects[j] = gameObjects[j + 1]; gameObjects[j + 1] = temp; } } } } ``` You can call the `SortGameObjects()` method in your `Update()` method to manually sort the game objects based on their distance to the target. ```csharp void Update() { // Check if the target and game objects are set if (target != null && gameObjects != null) { // Manually sort game objects based on distance to target SortGameObjects(); } } ```