- Home /
GUI Texture Transparency issue
Hi guys, just need some help on a tricky GUI problem I'm having at the moment. Basically its a battery-powered flashlight and I'm visually representing the battery power using GUI textures. Now, my problem is everytime I call the if argument, the GUI texture overlaps the previous which wouldnt be an issue but the draining battery is using a transparent alpha and so every GUI texture is overlapping the last preventing it from being shown below.
I'm also using a seperate script for the flashlight toggle and flickering effect hence the call midway through the script.
Here's the code:
#pragma strict
var batteryTexture100 : Texture2D;
var batteryMaterial100 : Material;
var batteryTexture90 : Texture2D;
var batteryMaterial90 : Material;
var batteryTexture80 : Texture2D;
var batteryMaterial80 : Material;
var batteryTexture70 : Texture2D;
var batteryMaterial70 : Material;
var batteryTexture60 : Texture2D;
var batteryMaterial60 : Material;
var batteryTexture50 : Texture2D;
var batteryMaterial50 : Material;
var batteryTexture40 : Texture2D;
var batteryMaterial40 : Material;
var batteryTexture30 : Texture2D;
var batteryMaterial30 : Material;
var batteryTexture20 : Texture2D;
var batteryMaterial20 : Material;
var batteryTexture10 : Texture2D;
var batteryMaterial10 : Material;
var batteryTexture0 : Texture2D;
var batteryMaterial0 : Material;
//The co-ordinates for the GUI texture -----------------------------------
var x : float =0;
var y : float = 0;
var w : float;
var h : float;
//------------------------------------------------------------------------
function Update ()
{
}
function OnGUI()
{
if (flashlight.currentBattPower == flashlight.maxBattPower)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture100);
}
if (flashlight.currentBattPower <= 9)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture90);
}
if (flashlight.currentBattPower <= 8)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture80);
}
if (flashlight.currentBattPower <= 7)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture70);
}
if (flashlight.currentBattPower <= 6)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture60);
}
if (flashlight.currentBattPower <= 5)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture50);
}
if (flashlight.currentBattPower <= 4)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture40);
}
if (flashlight.currentBattPower <= 3)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture30);
}
if (flashlight.currentBattPower <= 2)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture20);
}
if (flashlight.currentBattPower <= 1)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture10);
}
if (flashlight.currentBattPower <= 0)
{
GUI.DrawTexture(Rect(x, y, w, h), batteryTexture0);
}
I'm really eager to learn and I'm thinking maybe I might need this in an array format but I'm new to coding and not entirely sure how to call this in scripting...any help or pointers would be very much appreciated! Thanks guys!
Answer by Nerevar · Jun 17, 2014 at 12:09 PM
Hello,
As simple as this :
var currentTexture : Texture = null;
function OnGUI()
{
if (flashlight.currentBattPower == flashlight.maxBattPower)
{
currentTexture = batteryTexture100;
/* righ this in all your if statements with the right texture*/
}
//[..]
else currentTexture == null; //delete this line if you want to display the last texture even if none of your statements are true.
if(currentTexture)
GUI.DrawTexture(Rect(x, y, w, h),currentTexture);
}
you are an absolute legend. thank-you so much for this and also for the speedy reply!
Your answer
Follow this Question
Related Questions
help with Flashlight Pickup 1 Answer
Enable/Disable Scripts 3 Answers
Luz parpadeante con bateria 0 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers