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 kinggaunt · Oct 29, 2014 at 04:46 AM · guivariablefloatstatic

GUI.DrawTexture Error & Static Variables!!

Hi, if anybody can help me here, i want to create a GUI on screen from a script that is assigned into a light prefab. Please don't tell me to not put it there, the only other way i can combat that is if somebody told me how to use static variables where i declare the variable in one script (javascript) at the beginning of the script. The script is for a flashlight battery and it looks like this so far:

 var torchBattery0Percent : Texture;
 var torchBattery10Percent : Texture;
 var torchBattery20Percent : Texture;
 var torchBattery30Percent : Texture;
 var torchBattery40Percent : Texture;
 var torchBattery50Percent : Texture;
 var torchBattery60Percent : Texture;
 var torchBattery70Percent : Texture;
 var torchBattery80Percent : Texture;
 var torchBattery90Percent : Texture;
 var torchBattery100Percent : Texture;
 
 var weaponSelectHelp : Texture;
 
 private var weaponNumber : float = 0;
 public static var torchBattery : float = 1000;
 // Weapon Numbers:
 // 0 = Unarmed
 // 1 = Torch
 
 function Start (){
 
     GUI.DrawTexture(0.025f, 0.05f)(Rect(0,0,128,58), torchBattery100Percent);
 
 }
 
 function Update () {
 
     if(torchBattery > 1000){
         torchBattery = 1000;
     }
 
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
     // If you are unarmed and you press tab, equip the torch
     if(weaponNumber == 0 && (Input.GetKeyDown ("tab"))){
         light.intensity = 1;
         weaponNumber = 1;
     }
     else if(weaponNumber == 1 && (Input.GetKeyDown ("tab"))){
         light.intensity = 0;
         weaponNumber = 0;
     }
 
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
     if(weaponNumber == 1){
         torchBattery = torchBattery - 1;
     }
     else if(weaponNumber == 0){
         torchBattery = torchBattery + 1;
     }
     
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
     if(torchBattery <= 0){
         light.intensity = 0;
         weaponNumber = 0;
     }
 
 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
     if(torchBattery <= 100){
         
     }
 
 Debug.Log(String.Format("Torch Value = {0}", torchBattery)); //########################################### COMMENT THIS OUT WHEN DONE
     
 }

It is actually working fine in terms of the battery i want. BUT the thing that isn't working about it is the textures i have for the battery percent. If you look in the start function, the problem that is constantly been brought up is this:

BCE0023: No appropriate version of 'UnityEngine.GUI.DrawTexture' for the argument list '(float, float)' was found.

If anybody could help me with this i would be extremely thankful!!

Also if there is no way to combat this or it is a poor way to combat it, then what i would rather do is keep the variable 'torchBattery' as a public static variable (at the top with the rest of the declared variables) and access it from another script. THIS WOULD BE THE BEST WAY FOR ME but i just don't know how i would be able to do it.

I'd like to know how to do it, and i tried to make another script to access the public static variable which looks like this:

 var torchPercent : float = ChangeWeapon.torchBattery;
 
 function Update () {
 
 Debug.Log(String.Format("WOOOOOOOOOOAH = {0}", torchPercent));
 
 }

The only reason i did ChangeWeapon.torchBattery is because the other script is called ChangeWeapon.js and the public static variable that i want to access is called torchBattery.

If you could help me this way it would be better, but the other way would be fine too - figuring out the error on GUI.DrawTexture...

Comment
Add comment · Show 2
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 robertbu · Oct 29, 2014 at 05:30 AM 0
Share

Don't understand enough to recommend a 'communication' solution, but this:

  GUI.DrawTexture(0.025f, 0.05f)(Rect(0,0,128,58), torchBattery100Percent);


...is wrong 1) because GUI calls must be made in the OnGUI() function and made each frame you want the GUI to show and 2) the function call should be:

  GUI.DrawTexture(Rect(0,0,128,58), torchBattery100Percent);

Note consider putting your torchBattery*Percent textures in an array.

avatar image kinggaunt · Oct 29, 2014 at 12:36 PM 0
Share

But i need it to be permanently placed at a certain distance from the corner of the screen, regardless of the screen size. How would i do that? I also need it so that if the screen size is lower, the gui will get scaled down. The only other way i accomplished this was by using planes and materials. I don't know any other way, because the pixel inset on a gui texture makes the gui start to go off screen when scaled down?!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by kinggaunt · Oct 29, 2014 at 12:36 PM

But i need it to be permanently placed at a certain distance from the corner of the screen, regardless of the screen size. How would i do that? I also need it so that if the screen size is lower, the gui will get scaled down. The only other way i accomplished this was by using planes and materials. I don't know any other way, because the pixel inset on a gui texture makes the gui start to go off screen when scaled down?!

Comment
Add comment · 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
0

Answer by GrKl · Oct 29, 2014 at 01:58 PM

Hi,

I think you should document yourself a bit more on GUI in unity first. A LOT of good documentations exists. http://docs.unity3d.com/ScriptReference/GUI.DrawTexture.html

Also: you can scale a texture to fit your needs, so you don't need a different texture for every 10%. Also, to scale the size depending on the screen resolution: http://docs.unity3d.com/Manual/gui-Basics.html

Try this (untested, but should be ok):

 function OnGUI() {
     GUI.DrawTexture(Rect(10,10,128 /(1000/ torchBattery),58), torchBattery100Percent, ScaleMode.ScaleToFit);
 }

This would create a texture on top left of the screen (at 10 pixel from left and 10 pixel from top, regardless of the screen size), of a size 128 pixel proportionally scaled to you torchBattery variable (if I'm correct torchBattery at 1000 = 100% in your code)

Using this, you only use "torchBattery100Percent" and can delete all your other textures

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 kinggaunt · Oct 29, 2014 at 07:01 PM 0
Share

$$anonymous$$y solution was public static variables and using two scripts, but i thought doing what you did (set in by 10 pixels from top and 10 from the left) didn't work. Whenever i use pixel inset it literally NEVER works! :/ ONLY REASON being is because when the screen size decreases, the size of the GUI doesn't decrease.

So for example, if a screen size (just and example) was 1000x1000 and the gui was set in with a pixel inset of 10,10 meaning it would be 10 in from the left and 10 in from the top... If somebody selected another screen resolution such as 500x500, the actual size of the gui wouldn't change, if it was say 600x100 image you wanted as the gui, it would ALWAYS be a 600x100 image no matter what the screen resolution is, so if the resolution is scaled down too much, the gui will appear bigger than what i wanted it to be. How do i combat that?

So that it scales down when the screen resolution is scaled down (size)

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

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Public and Static Variables 2 Answers

Display GUI texture on specific variable value? 0 Answers

Static variables 1 Answer

GUI Variable Declaration problem 0 Answers


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