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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by DJ_Gregsta · Jun 17, 2014 at 11:14 AM · javascripttransparencyflashlight

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!

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

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);
 }
Comment
Add comment · Show 1 · 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 DJ_Gregsta · Jun 17, 2014 at 02:16 PM 0
Share

you are an absolute legend. thank-you so much for this and also for the speedy reply!

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

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

OnTriggerEnter not working for NavMesh 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