usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassHealthBar:MonoBehaviour{publicGameObjectcrystalPrefab;publicMaterialliveCrystal;publicMaterialdeadCrystal;publicintlives;privateintcurrentHealth;publicinthealth{get{returncurrentHealth;}set{currentHealth=value;updateCrystals();}}privateList<Renderer>crystals=newList<Renderer>();privatevoidupdateCrystals(){for(inti=0;i<lives;i++){if(health>i)crystals[i].material=liveCrystal;elsecrystals[i].material=deadCrystal;}}voidStart(){for(inti=0;i<lives;i++){GameObjectcrystal=Instantiate(crystalPrefab,transform.position+Vector3.right*0.5f*i,Quaternion.identity);crystals.Add(crystal.GetComponentInChildren<Renderer>());}health=lives;updateCrystals();}// Update is called once per framevoidUpdate(){}}