- Home /
put a void in an if statement?
Hey! im making a loading screen within Unity and i need to check if a continue button is pressed so the loading can start. Im Checking if the Continue button is pressed with a public void, but I get an error when putting:
if (ContinueButtonPressed) { (start loading) } when trying that I get the error "Cannot implicitly convert type void to bool"
ideas? here's my code so far:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SceneLoader : MonoBehaviour {
private bool loadScene = false;
public Button ContinueButton;
public Text FirstText;
public Text SecondText;
public Text ThirdText;
public Text loadingText;
[SerializeField]
private int scene;
[SerializeField]
private Text LoadingText;
public void ContinueButtonPressed()
{
FirstText.enabled = false;
SecondText.enabled = false;
ThirdText.enabled = false;
}
// Updates once per frame
void Update() {
// If the player has pressed the space bar and a new scene is not loading yet...
if (loadScene == false)
{
if (ContinueButtonPressed())
{
LoadingText.enabled = true;
// ...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 = Application.LoadLevelAsync(scene);
// 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;
}
}
}
Using a function that returns nothing in an if statement makes no sense. How would you expect C# to evaluate such a thing?
Im Checking if the Continue button is pressed with a public void
Your function "ContinueButtonPressed" is checking nothing at all. It's just disabling some text components. Perhaps you want that function to return a bool, and perhaps return if a key is pressed or not?
Answer by NeonTheCoder · Aug 14, 2017 at 04:52 AM
If you are trying to say when the function is called do something as in If(ContinueButtonPressed is called) { do stuff } you need to add a bool as a variable in the script with the default value of false and when the function is called set it to true and use the bool in the if. Otherwise use a C sharp delegate ex:
public delegate void ButtonPressed();
public event ButtonPressed OnButtonPressed;
void PressButton(){
OnButtonPressed();
//Other code here
}
void Start(){
OnButtonPressed += Stuff();
}
void Stuff(){
//Code in if statement here
}