Have button issues

Hey,
What im trying to do is have a button in the middle of the screen and once its pressed a progress bar shows up.And when you keep pressing the button, the progress bar should keep updating until it is full. I don’t know why when i press the button nothing shows up.Really need some help here…

Heres the code:

var Progressbar : Texture2D;
var ProgressbarImages : Texture2D[];

function OnGUI(){
  var ProgressImage = ProgressbarImages[-1];
  if(GUI.Button(Rect(300,100,100,20) ,"progress")){

    GUI.Label(Rect(370,400,560,10), ProgressImage);


  }

}

When you use the button, the label pops up for a tiny portion of a second but no more. In order to get it to stay up, you need to get the button to set a boolean to true and then make the label if the boolean is true.

How would you go about implementing the true boolean with the example code that i posted?

var cube3d : boolean;

function OnGUI(){
	if(GUI.Button(Rect(0,0,100,100),"C_me")) {
		cube3d=!cube3d;
	}
	
	if(cube3d){
		GUI.Label(Rect(0,0,100,100),"over");
	}
}

Thanks alot!!!

Got a bit carried away with this example:

// C# example
// NewBehaviourScript.cs
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
	public float progressBarWidth = 300.0f;
	float progress = 0.0f;
	
	void OnGUI()
	{
		GUILayout.BeginArea( new Rect( ( Screen.width - progressBarWidth + 12.0f ) / 2.0f, ( Screen.height - 100.0f ) / 2.0f, progressBarWidth + 12.0f, 100.0f ) );
			GUILayout.Label( "Progress bar test" );
			progress = ProgressBarTest( "Working", progress, progressBarWidth );
		GUILayout.EndArea();
	}
	
	public static float ProgressBarTest( string title, float progress, float width )
	// Since this is a static method, we can use it from just about anywhere - like so: progress = NewBehaviourScript.ProgressBarTest( title, progress, width );
	{
		Rect rect;
		Vector2 size;
		string label;
		
		progress = GUILayout.RepeatButton( "Increase" )  progress < 1.0f ? progress + 0.01f : progress;
			// Increase progress while the button is held
		
		// Render the bar //
		GUILayout.BeginHorizontal( GUI.skin.GetStyle( "Box" ), GUILayout.Width( width + 12.0f ) );
			GUILayout.Box( "", GUILayout.Width( width * progress ) );
		GUILayout.EndHorizontal();
		
		// Render the text within //
		rect = GUILayoutUtility.GetLastRect();
		label = title + " " + ( int )( progress * 100.0f ) + "%";
		size = GUI.skin.GetStyle( "Label" ).CalcSize( new GUIContent( label ) );
		rect = new Rect( rect.x + ( rect.width - size.x ) / 2.0f, rect.y + ( rect.height - size.y ) / 2.0f, size.x, size.y );
		GUI.Label( rect, label );
		
		// The reset button
		progress = GUILayout.Button( "Reset" ) ? 0.0f : progress;
		
		return progress;
	}
}

If you wish to add that the progress bar should be hidden when theres zero progress, just add in a if( progress > 0.0f ){ // } around where the progress bar is rendered.

Edit: Moved this to UnityGUI.

Forum tip:
When you post code on the forums use the Code button or manually wrap your code in a code block ([ code ]…[ /code ], without the spaces) for better formatting. I’m going to edit the posts above so you can see what that looks like for yourselves. Rock on y’all. :slight_smile:

Hey,
I ran accross a problem…Im still pretty new using unity but how do you use two triggered colliders in the same scene to do two different things?

p.s Thanks for Showing me how you would implement that code in C#.

Forget it…I figured it out.