Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DireTemplar57 · Nov 23, 2014 at 03:45 AM · javascriptgui

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;
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DireTemplar57 · Nov 24, 2014 at 01:33 AM 0
Share

I tried adding that line. It still hangs though…

avatar image robertbu · Nov 24, 2014 at 01:41 AM 0
Share

Change line 35 to:

    else if(alpha <= 0)

Actually would you should do, is replace lines 32 - 36 with:

    empty = (alpha > 0.0);
avatar image DireTemplar57 · Nov 24, 2014 at 03:07 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges