Having trouble deleting objects from a list after they reach a certain scale.
I am almost done with this project but am stuck on the last part. Basically, the parameters for what I'm working on is as follows:
- Instantiate one cube prefab every frame into the scene. 
- The cubes need to be named as “Cube1”, “Cube2”… based on the order they are generated. 
- The cubes need to be generated at a random locations within 1 unit from the origin [0,0,0]. 
- Each cube should have a random color. 
- The cube size (localScale) shrink 10% in each frame. 
- When the cube’s scale is less than 10% of its original scale, the cube is destroyed. 
 
I am stuck on six. The cubes are created, colored and named, and shrink over time, but will not destroy themselves after they shrink enough. I think I'm trying to reference the scale in the wrong way, because there is no console output about removing any cubes (which I put in there to debug)
Apologies if my code is sloppy, like I said I'm new to Unity and am trying my best haha. The troubled code bit is in the AddNewCube() function.
My code so far:
 using JetBrains.Annotations;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CubeCreator : MonoBehaviour
 {
     Vector3 origin;
     private List<GameObject> cubeList;
     public float targetScale = 0.1f;
     public float shrinkSpeed = 0.1f;
     // Start is called before the first frame update
 
 
 
 
     private void Awake()
     {
         cubeList = new List<GameObject>();
     }
 
     void Start()
     {
         origin = new Vector3(0, 0, 0);
 
     }
 
     // Update is called once per frame
     void Update()
     {
         AddNewCube();
     }
 
     private void SetRandCubeState(GameObject cube)
     {
 
         float randScale = Random.Range(0.8f, 1.0f);
         Vector3 randomScaler = new Vector3(randScale, randScale, randScale);
         cube.transform.localScale = randomScaler;
         Vector3 randomPosition = new Vector3(Random.Range(origin.x - 1.0f, origin.x + 1.0f), Random.Range(origin.y - 1.0f, origin.y + 1.0f), Random.Range(-1.0f,1.0f));
         cube.transform.localPosition = randomPosition;
         Renderer render = cube.GetComponent<Renderer>();
         render.material.SetColor("_Color", Random.ColorHSV());
 
     }
 
     private void AddNewCube()
     {
         GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cubeList.Add(newCube);
         SetRandCubeState(newCube);
         for (int i = 0; i < cubeList.Count; i++)
         {
             cubeList[i].name = "cube" + i;
             cubeList[i].transform.localScale = Vector3.Lerp(cubeList[i].transform.localScale, new Vector3(targetScale, targetScale, targetScale), Time.deltaTime * shrinkSpeed);
             if(cubeList[i].transform.localScale.x <= targetScale) 
             {
                 cubeList.RemoveAt(i);
                 RemoveCube(newCube);
                 print("cube " + i + " removed");
 
             }
         }
 
 
     }
 
     private void RemoveCube(GameObject oldCube)
     {
         Destroy(oldCube);
 
     }
 }
 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                