- Home /
Is opening a Canvas possible in StartCoroutine()?
Hi. I would like to know if that is possible. What I would like to do is when I click an image, it will change to another image and after 1 or 2 seconds, a Canvas will open. Here's a sample code I've using. And no, it doesn't work. Is something wrong with it? I'm sorry I'm not quite familiar with StartCoroutine yet. Thanks! PS: I've tried putting a variable "trigger" to initiate the opening of Canvas but still doesn't work.
IEnumerator Timer(){
bool trigger = false;
yield return new WaitForSeconds (5);
trigger = true;
Debug.Log ("start");
if (trigger == true) {
Canvas MainCanvas= GameObject.Find ("MainCanvas").GetComponent<Canvas> ();
MainCanvas.enabled = false;
}
}
Answer by HarshadK · Feb 26, 2015 at 12:10 PM
You don't need trigger. You just need to set enabled to true for your Canvas component.
IEnumerator Timer(){
yield return new WaitForSeconds (5f);
Canvas MainCanvas= GameObject.Find ("MainCanvas").GetComponent<Canvas> ();
MainCanvas.enabled = true;
}
Hi. Can I have follow up question? I've been thinking if this is possible. Can the function Timer run multiple statements? Like, if I click image1, the mainCanvas will open and if click image2, the subCanvas will open.
You can provide an argument to your coroutine based on which you can decide which Canvas or subCanvas to open.
I don't know how to get the variable needed. Please tell me how to get the parameters I've used for the function public void ClickedImage(int index)
index1, index2, and so on.
Can you provide the whole script so that it becomes more clear how you are doing it?
Your answer
