- Home /
Error CS0029: Cannot implicitly convert type void to type UnityEngine.AsyncOperation
I am trying to make a loading screen on a GUI button that currently says "New Game." Here are my two questions:
How can I get the code below to make the "New Game" text say "Loading?"
How can I fix the code to work without giving me errors about the "AsyncOperation" method?
Here's the code (it does use the Unity 5.3.1 syntax):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
private bool loadScene = false;
[SerializeField]
private int scene;
[SerializeField]
private Text loadingText;
// Updates once per frame
void Update()
{
// If a new scene is not loading yet...
if (loadScene == false)
{
// ...set the loadScene boolean to true to prevent loading a new scene more than once...
loadScene = true;
// ...change the instruction text to read "Loading..."
loadingText.text = "Loading...";
// ...and start a coroutine that will load the desired scene.
StartCoroutine(LoadNewScene());
}
// If the new scene has started loading...
if (loadScene == true)
{
// ...then pulse the transparency of the loading text to let the player know that the computer is still working.
loadingText.color = new Color(loadingText.color.r, loadingText.color.g, loadingText.color.b, Mathf.PingPong(Time.time, 1));
}
}
// The coroutine runs on its own at the same time as Update() and takes an integer indicating which scene to load.
IEnumerator LoadNewScene()
{
// This line waits for 3 seconds before executing the next line in the coroutine.
// This line is only necessary for this demo. The scenes are so simple that they load too fast to read the "Loading..." text.
yield return new WaitForSeconds(3);
// Start an asynchronous operation to load the scene that was passed to the LoadNewScene coroutine.
AsyncOperation async = SceneManager.LoadScene("TOY test");
// While the asynchronous operation to load the new scene is not yet complete, continue waiting until it's done.
while (!async.isDone)
{
yield return null;
}
}
}
I haven't worked with the AsyncOperation yet. But I can possibly answer the question on changing the text to say loading. I believe you have to reference the text you're trying to change, because I don't see where it's at in your script. One way you can do it is Like in the start function add LoadingText=GameObject.Find("TheNameofTheCompentThatHasTheTextOnIt").GetComponent<Text>();
and that should work for the text
Answer by FFVDGames · Feb 02, 2016 at 05:32 PM
I figured out the answer. GAMZER0, I did figure out how to do the text thing with what you said. I ended up removing the new Color stuff (the fading), and just made a void method that is called in the IEnumerator that just loads the scene. Apparently, SceneManager.LoadScene(); can't go into an IEnumerator... Oh well!
Nice, Glad to help, and glad you have figure it out. Now I know you can't put the Scene$$anonymous$$anager load in IEnumerator.
Your answer
Follow this Question
Related Questions
Unity 2D - Text UI and Textbox scripting issue - HELP ASAP! 2 Answers
create a button with name and text for every game object with tag 0 Answers
How can I apply a text variable to a prefab 2 Answers
Make text appear and stay when clicking. 1 Answer
Can't see text for menu in unity with a texture placed 1 Answer