loadingscreen scripting

hello everyone,

i made a scene with th following components:

2861385--209408--upload_2016-11-23_13-33-42.png
the canvas contains a
-imagebackground
-button (which i want to replace to: press enter of f (something like that)
-imagebackground2 (which appears when i press the button.)
-slider. (loading bar)(appears when pressed the button)
-loadingtext (appears when pressed the button)

i attached the script below to the button.
(the right way)

what i want: a scene with a loading screen.
once i press the button. a load bar should appear.
a text “loading…” should appear.
what is the reason it isnt working? i get an error(below↓)


here is the script

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class loadingbar : MonoBehaviour {

    AsyncOperation ao;
    public GameObject LoadingScreenBG;
    public Slider progBar;
    public Text loadingtext;

    public bool isFakeLoadingbar = false;
    public float fakeIncrement = 0f;
    public float fakeTiming = 0f;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

    public void LoadLevelammp()
    {
        LoadingScreenBG.SetActive(true);
        progBar.gameObject.SetActive(true);
        loadingtext.gameObject.SetActive(true);
        loadingtext.text = "loading...";

        if (!isFakeLoadingbar)
        {
            StartCoroutine(LoadLevelWithRealProgress());
        }
        else
        {

        }

    }

    IEnumerator LoadLevelWithRealProgress()
    {
        yield return new WaitForSeconds(1);

        ao = SceneManager.LoadSceneAsync(1);
        ao.allowSceneActivation = false;

        while (!ao.isDone)
        {
            progBar.value = ao.progress;

            if(ao.progress == 0.9f)
            {
                loadingtext.text = "press start to continue";
                if (Input.GetKeyDown(KeyCode.Joystick1Button7))
                {
                    ao.allowSceneActivation = true;
                }
            }
            Debug.Log(ao.progress);
            yield return null;
           
        }
    }
}

now im getting the error:
NullReferenceException: Object reference not set to an instance of an object
loadingbar+c__Iterator0.MoveNext () (at Assets/loadingbar.cs:50)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

hope someone could help me :smile:

“ao” is null at line 50… if it’s an async operation you might find it’s only populated once the load has completed, if so you’ll want to check if it’s null and yield until it isn’t.

thanks for your reply i got it working already! thanks anyway