- Home /
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 :/
$$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.
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.
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
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);
}
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
Your answer
Follow this Question
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