- Home /
 
Unity freezes when changing GUI.Color.a
Hi, I'm working on a school project and I trying to make a DMC devil trigger style UI, you know those circular things then fade away when you activate devil trigger in dmc. I've tried using this method where there is a controller script for the meter, and there is a GUI script for each of the circles itself. I've encountered this problem where unity always freezes when I set enrage to true. I would like to ask for a solution to this problem if there is a problem in my script, or if there is a better way to accomplish this. Thanks in advance!
This is my rageMeterController Script:
 var rageMeterPrefab: GameObject;    //the prefab to instantiate
 var rageMeter: RageMeterScript[];    //the array to grab all scripts from the rage meter game objects
 var rageCount: int;    //the number of circles
 var enraged: boolean;    //is the player in "rage" mood?
 var empty: boolean;    //is the current circle empty?
 
 function Start () 
 {
     rageCount = GameControl.gameController.pRage;    //grabs the number of circles from the Game Controller
     rageMeter = new RageMeterScript[rageCount];    //creates a new array according to the rageCount variable
     for(var i: int = 0; i < rageCount; i++)
     {
         var rage = Instantiate(rageMeterPrefab, transform.position, Quaternion.identity);    //instantiates the prefab to display the circle
         rage.transform.parent = gameObject.transform;    //parents the circle to this game object
         rageMeter[i] = rageMeterPrefab.GetComponent(RageMeterScript);    //assigns the script of each instantiated game object to a position in the array
         rageMeter[i].rageCount = i;    //gives each circle its number to change its position by the offset
     }
 }
 
 function OnGUI()
 {
     
     if(GUI.Button(Rect(Screen.width/2 - 50, Screen.height/2 - 50, 100, 100), "enrage"))    //tempo button to enrage
     {
         enraged = !enraged;    //enrage!!!
     }
     if(enraged)
     {
         //supposedly, starting at the last circle, check if it's empty. If it isn't, decrease its alpha. If it is, move to the next circle.
         for(var i: int = rageCount - 1; i > 0;)
         {
             empty = rageMeter[i].empty;
             if(empty)
                 i--;
                 
             while(!empty)
                 rageMeter[i].decreaseAlpha();
                 
                 
         }
     }    
 }
 
               This is my rageMeter Script:
 var rageCount: int;    //number of circles
 var alpha: float;    //alpha for the color
 var offSet: float;    //offset for the display of the circle
 var displayTexture: Texture2D;    //the Texture to display
 var empty: boolean;    //is the circle empty
 private var guiColor: Color;    //to contain the guicolor
 
 function Start () 
 {
     alpha = 1;    //sets alpha to 1
 }
 
 function OnGUI()
 {
     //sets the GUI color to the variable
     GUI.color = guiColor;
     //Creates the circle with offset
     GUI.DrawTexture(Rect (Screen.width/2 + 50 + (rageCount* offSet), Screen.height - 35, 25, 25), displayTexture, ScaleMode.StretchToFill, true, 0);
 }
 
 
 //this function decreases alpha when the player is in "rage" mode
 function decreaseAlpha()
 {
     alpha -= 0.1 *Time.deltaTime;
 }
 
 function Update () 
 {
     Mathf.Clamp(alpha, 0, 1);    //clamps alpha to 0 and 1
     guiColor.a = alpha;    //sets the a value of the color variable to alpha
     if(alpha > 0)    
         empty = false;    //sets empty to false when alpha is more than 0;
         
     else if(alpha == 0)
         empty = true;    //sets empty to true when alpha is 0;
     
     
     //alpha -= 0.1 *Time.deltaTime;
 }
 
              Answer by robertbu · Nov 23, 2014 at 03:50 AM
Your problem is here:
  while(!empty)
        rageMeter[i].decreaseAlpha();
 
               You don't get the new value of empty within the loop, so this code will hand Unity. One fix to the hang is:
  while(!empty) {
        rageMeter[i].decreaseAlpha();
        empty =  rageMeter[i].empty;
  }
 
               But that doesn't fix the script. In particular this logic will cause the rageMeter to decrease it alpha all within a single frame, which I don't believe is what you want. I wonder if just leaving out the while() on line 36 won't get you what you want.
Change line 35 to:
    else if(alpha <= 0)
 
                  Actually would you should do, is replace lines 32 - 36 with:
    empty = (alpha > 0.0);
 
                 Never$$anonymous$$d I solved it. I added a new variable to keep track of the current circle that was being accessed ins$$anonymous$$d of using a loop, it stopped the hang and does exactly what I want now.
 var rage$$anonymous$$eterPrefab: GameObject;    //the prefab to instantiate
 var rage$$anonymous$$eter: Rage$$anonymous$$eterScript[];    //the array to grab all scripts from the rage meter game objects
 var rageCount: int;    //the number of circles
 var currentCount: int;    //the current circle in the BuiltIn Array
 var enraged: boolean;    //is the player in "rage" mood?
 var empty: boolean;    //is the current circle empty?
 
 function Start () 
 {
     rageCount = GameControl.gameController.pRage;    //grabs the number of circles from the Game Controller
     rage$$anonymous$$eter = new Rage$$anonymous$$eterScript[rageCount];    //creates a new array according to the rageCount variable
     currentCount = rageCount - 1;
     for(var i: int = 0; i < rageCount; i++)
     {
         var rage = Instantiate(rage$$anonymous$$eterPrefab, transform.position, Quaternion.identity);    //instantiates the prefab to display the circle
         rage.transform.parent = gameObject.transform;    //parents the circle to this game object
         rage$$anonymous$$eter[i] = rage.GetComponent(Rage$$anonymous$$eterScript);    //assigns the script of each instantiated game object to a position in the array
         rage$$anonymous$$eter[i].rageCount = i;    //gives each circle its number to change its position by the offset
     }
 }
 
 function OnGUI()
 {
     
     if(GUI.Button(Rect(Screen.width/2 - 50, Screen.height/2 - 50, 100, 100), "enrage"))    //tempo button to enrage
     {
         enraged = !enraged;    //enrage!!!
     }
     if(enraged)
     {
         //Added this!
         if(empty && currentCount > 0)
         {
             currentCount--;
             empty = rage$$anonymous$$eter[currentCount].empty;
         }
         
         else
             rage$$anonymous$$eter[currentCount].decreaseAlpha();
         
     }    
 }
 
 function Update () 
 {
     empty = rage$$anonymous$$eter[currentCount].empty;
 }
                 Your answer
 
             Follow this Question
Related Questions
What Am I Doing Wrong? Variable Names 3 Answers
Created game objects rollover show guy text 0 Answers
Set dragging boundaries for a self-made slider 1 Answer
Convert Timer to HH:MM:SS 1 Answer
Unity Account system 1 Answer