Power charger script problem

I have a problem with my script, so I would appreciate if someone could help me.

It is a power charger. It should go automatic from 1 to 200, and thend go back from 200 to 1. Here’s the code:

var att1 : int = 10;

function Update() {

do  {att1+=Time.time;}
while(att1<=200);

if(att1==200){
do {att1-=Time.time;}
while(att1>=0);}

}

function OnGUI () {
GUI.Box(Rect(155,202.8,att1*2,15),"");
}

Every time I run it, it goes from 1 do 200 and then whole Unity freezes

first declare your custom state for this pattern/statemachine

public enum PowerState
{
   PowerUp,
   PowerDown
   // easily add more states if you need them here
}

PowerState chargerState = PowerState.PowerUp;

in your update or where you need to check it, use a SWITCH instead of IF. Because its easier to maintain and build out with multiple states

switch (chargerState)
{
case PowerState.PowerUp:
   att1 += Time.deltaTime;
   if (att1 >=200) chargerState = PowerState.PowerDown;
break;

case PowerState.PowerDown:
   att1 -= Time.deltaTime;
   if (att1 <0) chargerState = PowerState.PowerUp;
break;

//  add more states if you want more interesting patterns

default: // if no other conditions are true
    Debug.Log("unknown state: " + chargerState);
break; // always remember to end with a break pr. case, also default case

}

That is because you are getting stuck in a while loop. You dont need “while” in this case.

Poweringup : boolean = true;

if(att1 <=0)
{
Poweringup = true;
}

if(Poweringup)
{
att1+=1*Time.deltaTime;
}
if (att1 >=200)
{
Poweringup = false;
}
if(!Poweringup)
{
att1-=1*Time.deltaTime;
}