Gui placement help

Hi!

I have a problem with my flashlight script for placing an energy bar by using GUI.DrawTexture.
In Unity it looks good (check image below).

Note! The flashlight bar is the green one! The yellow one is a stamina bar!

25211-pic1.png

But when I’m playing the game after “Building” it it looks different for different resolutions (check images below).

The script

var BatteryTexture : Texture;
var border : Texture;
var lightSource : Light; //Connect the light source in the Inspector
var FlashlightSound : AudioClip;
static var energy : float = 0.0; //The energy amount of the flashlight
private static var turnedOn : boolean = false; //Boolean to check whether it's turned on or off
var drainSpeed : float = 0.4; //The speed that the energy is drained

function Update () 
{
    if (Input.GetKeyDown(KeyCode.F)) ToggleFlashlight();

	if (Input.GetKeyDown(KeyCode.F)) 
	{
		audio.PlayOneShot(FlashlightSound);
	}
    
}



//When the player press F we toggle the flashlight on and off
function ToggleFlashlight () 
{
    turnedOn=!turnedOn;
    if (turnedOn && energy>0) 
    {
       TurnOnAndDrainEnergy();
    } 
    else 
    {
       lightSource.enabled = false;
    }
    
}


function OnGUI ()
{
    GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
    GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );
    
	
}

//When the flashlight is turned on we enter a while loop which drains the energy
function TurnOnAndDrainEnergy () 
{
    lightSource.enabled = true;
    
    while (turnedOn && energy>0) 
    {
       energy -= drainSpeed*Time.deltaTime;
       yield;
    }
    lightSource.enabled = false;
}

//This is called from outside the script to alter the amount of energy
static function AlterEnergy (amount : int) 
{
    energy = Mathf.Clamp(energy+amount, 0, 100);
    
}

Do any of you guys know how to fix this?

Your dimensions for the rects are hardcoded:

GUI.DrawTexture(new Rect( 30, Screen.height - 880, 4.05 * Flashlight.energy, 25 ), BatteryTexture );
GUI.DrawTexture(new Rect( 30, Screen.height - 880, 405, 25 ), border );

Your border texture is 405 pixels regardless the screen dimensions.

To fix it use the screen dimensions to set your rect:

var width = Screen.width / ratioWidth;
var height = Screen.heighr / ratioHeight;
var xPos = Screen.width / marginRatio;
var yPos = Screen.height / heightRatio;

var rect :Rect = Rect(xPos, yPos, width, height);