Powerbar [C#]

Hi everyone.

I’m trying to do an power bar that lets player to jump higher longer he keeps space pressed down. I can see the powerbar growing when I hold space down, this is where the first problem lies. It can go over the full width i’ve set in my code.

Then we can continue to second problem, no matter how long I hold space down, player won’t jump higher.

Here’s the code so far. Sorry for not commenting it at all.

using UnityEngine;
using System.Collections;

public class JumpBar : MonoBehaviour {
	
	public float fullWidth = 256;
	
	private float thePower;
	
	public bool increasing = false;
	
	public bool jumping = false;
	
	public float barSpeed = 25;
	
	private GameObject player;
	

	// Use this for initialization
	void Start () {
	player = GameObject.Find("Player");
	
	// Set the jumping power bar at zero at start
	Rect pos = guiTexture.pixelInset;
	pos.xMax = guiTexture.pixelInset.xMin + barSpeed * thePower;
	guiTexture.pixelInset = pos;
		
	}
	
	// Update is called once per frame
	void Update () {
		
		if(!jumping && Input.GetButtonDown("Jump")) {
			increasing = true;
		}
		
		if(!jumping && Input.GetButtonUp ("Jump")) {
			increasing = false;
			player.rigidbody.AddForce(Vector3.up * thePower);
		}
		
		if(increasing) {
			thePower += Time.deltaTime * barSpeed;
			thePower = Mathf.Clamp (thePower, 0, fullWidth);
			
			Rect pos = guiTexture.pixelInset;
			pos.xMax = guiTexture.pixelInset.xMin + barSpeed * thePower;
			guiTexture.pixelInset = pos;
	
		}
		if(increasing == false) {
			thePower = 0;
			Rect pos = guiTexture.pixelInset;
			pos.xMax = guiTexture.pixelInset.xMin - barSpeed * thePower;
			guiTexture.pixelInset = pos;	
	}
}
}

So if someone could tell me where I’ve gone wrong? I think this is a really simple problem, but I haven’t been sleeping for two nights so my brains won’t really work at all right now and can’t seem to find where I’ve gone wrong.

Here’s the full working code now, so I mark this as a right answer, in case anyone else needs help with this kind of stuff, even though I think this might not be the best way or even correct way of doing this, but hey, after all it works and that’s what matters for me.

using UnityEngine;
using System.Collections;

public class JumpBar : MonoBehaviour {
	
	public float fullWidth = 256; //max width of the powerbar
	
	private float thePower; //current power
	
	public bool increasing = false;
	
	private bool jumping = false;
	
	public float barSpeed = 25; //how fast bar will fill in.
	
	
	private GameObject player; //player gameobject
	
	private GameObject _GameManager; //gamemanager gameobject
	

	// Use this for initialization
	void Start () {
	player = GameObject.Find("Player"); //finding player gameobject so we can make it jump.
	_GameManager = GameObject.Find("_GameManager"); //finding _GameManager so we can use sounds.
		
	}
	
	// Update is called once per frame
	void Update () {
		
		
		if(!jumping)
		{
			if(Input.GetButton("Jump")) { //if jump button is held down se increasing to true.
				increasing = true;
			}
				else if(Input.GetButtonUp ("Jump")) {
				player.rigidbody.AddForce (Vector3.up * thePower * 5); //add force to the player so it jumps.
				_GameManager.GetComponent<GameManager>().BallJump(); //after player jumps, play "jump" sound.
				increasing = false; //set bar increasing back to false.
				
			}
		}
		
		Rect pos = guiTexture.pixelInset;
		pos.xMax = guiTexture.pixelInset.xMin + fullWidth*thePower/fullWidth;
		guiTexture.pixelInset = pos;
		
		if(increasing) //if bar is increasing, calculate thepower
		{	
			
			thePower += Time.deltaTime * barSpeed;
			thePower = Mathf.Clamp (thePower, 0, fullWidth);

		}
		else //else set thepower back to 0.
		{
			thePower=0;
		}
	}
}

Thanks goes to Hoeloe, getyour411 for helping me out with this one.

[EDIT]
Added in bonus tips from Hoeloe. So it should be better.