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 KaoticBreedX · Oct 14, 2014 at 03:35 AM · guitexturealphafadefadeoutfadein

Changing a GUITexture's alpha gives me no effect in game.

I am changing 2 GUITextures alphas successfully, but it does not translate in game. I am trying to fade 2 textures in and out based on the distance between the camera and the object that has the textures anchored above it.

Here is the code in C#:

 using UnityEngine;
 using System.Collections;
 
 public class ActionHealthBarManager : MonoBehaviour {
 
     public static ActionHealthBarManager Instance;
     public GUITexture HealthBarFrame;
     public GUITexture HealthBar;
     public float MaxHealth = 250f;
     public float Y_Offset = 50f;
     public float minDistance = 1;
     public float FadeOutStartDistance = 45;
     public float FadeOutEndDistance = 50;
 
     private float currentHealth = 250f;
     private float healthBarPercentage = 1f;
     private bool isVisible;
 
     void Awake ()
     {
         Instance = this;
     }
 
     void OnGUI () 
     {
         DrawActionHealthBar ();
     }
 
     void DrawActionHealthBar ()
     {
         Vector3 objectScreenPosition = Camera.main.WorldToScreenPoint (transform.position);
 
         if (isVisible)
         {
             GUI.DrawTexture (new Rect (objectScreenPosition.x - 75, Screen.height - objectScreenPosition.y - Y_Offset, 150, 12), HealthBarFrame.texture);
             GUI.BeginGroup (new Rect (objectScreenPosition.x - 40, Screen.height - objectScreenPosition.y - Y_Offset, 115 * healthBarPercentage, 12));
             GUI.DrawTexture (new Rect (-35, 0, 150, 12), HealthBar.texture);
             GUI.EndGroup ();
             GUI.Label (new Rect (objectScreenPosition.x - 75, Screen.height - objectScreenPosition.y - Y_Offset - 5, 50, 20), healthBarPercentage * 100 + "%");
         }
 
         if (objectScreenPosition.z > FadeOutStartDistance && isVisible)
         {
             var thisTexture1 = HealthBarFrame.color;
             var thisTexture2 = HealthBar.color;
             thisTexture1.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
             thisTexture2.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
             HealthBarFrame.color = thisTexture1;
             HealthBar.color = thisTexture2;
         }
         
         isVisible = (objectScreenPosition.z >= minDistance && objectScreenPosition.z <= FadeOutEndDistance);
     }
 
     public void ProcessDamage (float damageAmmount)
     {
         currentHealth -= damageAmmount;
         healthBarPercentage = currentHealth / MaxHealth;
 
         if (currentHealth <= 0)
         {
             currentHealth = 250f;
             healthBarPercentage = 1f;
         }        
     }
 }

I am very confused :/

Comment
Add comment · Show 7
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 MrSoad · Oct 14, 2014 at 12:14 PM 0
Share

$$anonymous$$ight be wrong here but it looks like you are setting the alpha value outside the range 0 - 1 Although in the Unity editor it is from 0 - 255 when scripting it needs to be 0 - 1. Convert your values to a percentage on 1.0 Hope this fixes your problem.

avatar image KaoticBreedX · Oct 14, 2014 at 07:05 PM 0
Share

I wrote an in depth answer, but I think I just deleted it haha. Basically, I was saying that lines 46 and 47 does assign a value between 0 and 1. I put a Debug.Log (HealthBarFrame.color.a); between line 48 and 49 and it returns the correct values.

avatar image KaoticBreedX · Oct 14, 2014 at 07:57 PM 0
Share

Line 46 and 47 is where the alpha value gets modified:

 thisTexture1.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
 thisTexture2.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);

Basically I fade out the textures from FadeOutStartDistance which is 45 to FadeOutEndDistance which is 50. objectScreenPosition.z is the distance between the camera and the object that has the textures anchored above it. objectScreenPosition.z - FadeOutStartDistance gives me a number between 0 and 5 which is the current distance within the fade out range. It then divides it by the full range which is 5, which gives me a value from 0 to 1. I subtract that value to 1 to invert the fading effect, otherwise it would fade in as I get away from the object.

To be absolutely 100% sure I put a Debug.Log (HealthBarFrame.color.a); between line 48 and 49 and the alpha value was properly changing from 1 to 0 in the 45 to 50 distance range.

Thank you nonetheless :3

avatar image MrSoad · Oct 14, 2014 at 08:02 PM 0
Share

Ok try something like this. I can't guarantee the syntax is right, I'm a JS guy. But it looked like you were over complicating it so :

     if (objectScreenPosition.z > FadeOutStartDistance && isVisible) {
         HealthBarFrame.color.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
         HealthBar.color.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
     }
     
     if (objectScreenPosition.z > FadeOutStartDistance && isVisible) {
         HealthBarFrame.color.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
         HealthBar.color.a = 1.0f - (objectScreenPosition.z - FadeOutStartDistance) / (FadeOutEndDistance - FadeOutStartDistance);
     }
avatar image KaoticBreedX · Oct 14, 2014 at 08:21 PM 0
Share

That's what I had to begin with, but it gives me an error so I had to change it to what I'm using now.

Here's the error:

error CS1612: Cannot modify a value type return value of 'UnityEngine.GUITexture.color'. Consider storing the value in a temporary variable

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Fading out doesn't fade out, but instantly goes from black to clear 0 Answers

Fading out GUITexture - I need help with timer function that will control the speed of fading out 1 Answer

How can I fade in/out an object nonstop automatic ? 2 Answers

Fade In / Out UI Image 3 Answers

Fade in/out a GameObject 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