- Home /
making a progress bar in javascript
the bar is not filling up and also one number is displayed in textblock and level loads..
code:
#pragma strict
var levelname:String;
var loadingscreen:GameObject;
var emptybar:GameObject;
var bar:GameObject;
var percentage:GameObject;
var oldscreen:GameObject;
private var loadprogress:int=0;
function Start () {
loadingscreen.active=false;
emptybar.active=false;
bar.active=false;
percentage.active=false;
}
function Update () {
if(Input.GetKey(KeyCode.Space))
press();
if(Input.touchCount > 0)
{
var touch: Touch = Input.touches[0];
if(touch.phase == TouchPhase.Began && guiTexture.HitTest(touch.position)){
press();
}
}
}
function press()
{
this.guiTexture.pixelInset.width=-8;
this.guiTexture.pixelInset.height=-8;
yield WaitForSeconds(.2);
this.guiTexture.pixelInset.width=0;
this.guiTexture.pixelInset.height=0;
yield StartCoroutine("load");
}
function load()
{
oldscreen.active=false;
loadingscreen.active=true;
emptybar.active=true;
bar.active=true;
percentage.active=true;
var async:AsyncOperation=Application.LoadLevelAsync("level 1");
while(async.progress<=1)
{
Debug.Log(async.progress*100);
loadprogress=async.progress*10;
bar.transform.localScale=Vector3((loadprogress/3),bar.transform.localScale.y,bar.transform.localScale.z);
percentage.guiText.text="Loading "+loadprogress+" %";
yield null;
}
}`enter code here`
Answer by VanZeebroeck · Dec 16, 2014 at 10:03 AM
You are using yield in your press method. That makes it a co routine, and co-routines always have to be called with StartCoroutine, or they will do nothing. Try this:
StartCoroutine(press());
http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html
Also if I look at your mobile version of your input it seems you will want to use Input.GetKeyDown(KeyCode.Space), this way your press method will only be executed once every time you push the space bar, unless it is meant to be continuous update as long as the user holds the space bar down.
Your answer

Follow this Question
Related Questions
The best way to damage an enemy (js) 2 Answers
Error with Camera WorldtoScreenPoint 1 Answer
How to make a scene load more random than others? 1 Answer
Trying to pick one or the other 1 Answer
Disable/Enable Colliders 1 Answer