IEnumerator when button is pressed
I have this code: [code] using UnityEngine; using System.Collections;
public class Fading : MonoBehaviour {
public Texture2D fadeOutTexture;
public float fadeSpeed;
private int drawDepth = -1000;
private float alpha = 1.0f;
private int fadeDir = -1;
void OnGUI () {
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01 (alpha);
GUI.color = new Color (GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = drawDepth;
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeOutTexture);
}
public float BeginFade (int direction) {
fadeDir = direction;
return ( fadeSpeed );
}
void Start () {
}
void Update () {
}
void OnLevelWasLoaded () {
BeginFade (-1);
}
} [/code]
and I use with this: [code] IEnumerator FadeToNextLevel () { float fadeTime = GameObject.Find ("_GM").GetComponent ().BeginFade (1); yield return new WaitForSeconds (fadeTime); SceneManager.LoadScene("Test"); } [/code]
_GM is GameObject where I putted Fading script. How can I start IEnumerator when button is pressed? I want to use this code for fading between scenes, when button "New Game" is pressed, fade to new scene. o_O
I am not sure what your question is. Do you want to know how to call upon an IEnumerator? Or do you want to know how to check if the player presses the button "New Game"?
By the way, it seems to me you've forgotten to indicate which component GetComponent needs to get. I think you should change
GameObject.Find ("_G$$anonymous$$").GetComponent ().BeginFade (1);
to
GameObject.Find ("_G$$anonymous$$").GetComponent<Fading> ().BeginFade (1);
Cheers!
Ruben
Your answer
Follow this Question
Related Questions
Trying to increment a variable in a Coroutine on pressing a UI button 0 Answers
Can Unity throw an error when I start a coroutine without StartCoroutine? 0 Answers
Script loads wrong information 0 Answers
How can i get my enemy spawner follow the player? 0 Answers
Public IEnumerator wont work if attached to a button. 0 Answers