- Home /
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...
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.
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?!
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?!
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
$$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
Follow this Question
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