- Home /
Loading Bar / LoadLevelAsync
As you can see in my code, I have attempted to create my own Loading Bar using an image's fillAmount value of Horizontal left to right.
So far, I am getting an error for:
LoadingBar.fillAmount = async.progress.ToString ();
"Cannot implicity convert a 'string' to 'float'"
Also this kind of code that I have created, will it even work? I am new to all this Loading bar stuff.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ExampleClass : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start() {
async = Application.LoadLevelAsync("L2Easy");
//async.progress = LoadingBar.fillAmount;
yield return async;
}
void Update()
{
LoadingBar.fillAmount = async.progress.ToString ();
if (LoadingBar.fillAmount >= 1)
{
StartCoroutine(LoadLevel());
}
}
IEnumerator LoadLevel()
{
yield return async;
}
}
I got rid of .ToString() and it fixed it, and now to see if the code will actually work.
Answer by DimitriUK · Jul 13, 2015 at 02:20 PM
Funny how I just wrote this question and now I am answering it.
IF YOU ARE LOOKING FOR A WORKING LOAD BAR
'Load Level with out a button pressed'
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadLevel : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start()
{
yield return new WaitForSeconds (0.1f);
async = Application.LoadLevelAsync ("L2Easy");
}
void Update()
{
LoadingBar.fillAmount = async.progress;
}
}
'Load Level with a button pressed'
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class LoadLevel : MonoBehaviour {
public Image LoadingBar;
private AsyncOperation async;
IEnumerator Start()
{
yield return new WaitForSeconds (0.1f);
async = Application.LoadLevelAsync ("L2Easy");
async.allowSceneActivation = false;
}
void Update()
{
LoadingBar.fillAmount = async.progress;
if (LoadingBar.fillAmount == 0.9f)
{
if(Input.GetKeyDown(KeyCode.Space))
{
async.allowSceneActivation = true;
}
}
}
}
Insert this script on to any game object so when your level finishes, it activates the gameobject that holds this script.
Assign your UI Image and change the Image Type to Filled and set the Fill Amount to 0.
You are done! Now when this gameobject activates, it begins the loading process.
Answer by WILEz1975 · Aug 30, 2016 at 10:03 PM
You need to add if(async!=null) because the first update cycle occurs before the "WaitForSeconds (0.1f);"
void Update () {
if(async!=null)LoadingBar.fillAmount=async.progress;
}
Your answer

Follow this Question
Related Questions
Display spinning icon while some search method in progress ? 0 Answers
IEnumerator not working 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
GTA like weapon switch system 0 Answers