I have setup code for a loading screen, which works and loads the next scene when I press any key. However the slider does not move. I am uncertain if I did something wrong, I followed a tutorial on how to make a loading screen but removed the button that triggers it.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
// Use this for initialization
void Start()
{
StartCoroutine(AsynchronousLoad(1));
}
// Update is called once per frame
void Update()
{
}
bool canPlayerFire = true;
IEnumerator coRoutineTest()
{
Debug.Log("Fire");
canPlayerFire = false;
yield return new WaitForSeconds(3);
canPlayerFire = true;
}
IEnumerator AsynchronousLoad(int lvl)
{
yield return null;
AsyncOperation ao = SceneManager.LoadSceneAsync(lvl);
ao.allowSceneActivation = false;
while (!ao.isDone)
{
// [0, 0.9] > [0, 1]
float progress = Mathf.Clamp01(ao.progress / 0.9f);
// Loading completed
if (ao.progress == 0.9f)
{
Debug.Log("Press a key to start");
if (Input.anyKey)
ao.allowSceneActivation = true;
}
yield return null;
}
}
}
Like I said the slider doesn’t move at all, any help to fix this would be appreciated.
Well I thought I had gotten it fixed up, I re wrote the code per the tutorial exactly as I saw on youtube. However when I run it with the loading screen object and slider set in the unity inspector as shown
and when I click play, my slider still does not move, This is my rewritten code. I do not know if I am missing something or not but upon investigation it looks like I did everything right.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class NewBehaviourScript : MonoBehaviour
{
public GameObject LoadingScreenObj;
public Slider slider;
AsyncOperation async;
void start()
{
}
public void LoadingScreenexample()
{
StartCoroutine(LoadingScreen(0));
}
IEnumerator LoadingScreen(int lvl)
{
LoadingScreenObj.SetActive(true);
async = SceneManager.LoadSceneAsync(lvl);
async.allowSceneActivation = false;
while (async.isDone == false)
{
slider.value = async.progress;
if (async.progress == 0.9f)
{
slider.value = 1f;
async.allowSceneActivation = true;
}
yield return null;
}
}
Not sure, looks better this time. If you debug log the async progress do you get more than 1 value (ie: is it going by so fast you can’t see it? The second code you posted doesn’t include the Input.anyKey from your previous post - unless you just left that out on the forum…
When I saw the method calling the coroutine, I imagined he was calling it from elsewhere.
So, you’re saying you don’t see any debug output in the console at all?
Did you fix the typo for Start (shouldn’t be ‘start’) in your personal copy of the code?
No problem. And of course you don’t have to call it from Start… ( I mean, maybe in your code it makes the most sense). What you do have to do, though, is call it somehow