- Home /
IEnumerable called within a EventHandler. Can this be done?
I'm currently working on making my own GUIManger type system. I'm trying to find a way to pop a notification up on screen and have it go away after five seconds. I have found some ways to do this such as using Invoke(), but I would really like to get it working with delegates and an IEnumerable as I feel like this would be a much cleaner way to deal with it. Here's the code that I currently have. The only problem is that it currently doesn't fire the notification function like it should be doing. onNotification() is triggered, but notification() is never ran. I'm pretty sure I'm misunderstand how something works because this topic is newer to me. using UnityEngine; using System.Collections; using System;
public class GUIView : MonoBehaviour {
public Texture barTexture;
public Texture lightBarTexture;
public ObjectIneraction objectInteraction;
ArrayList notifications = new ArrayList();
public event EventHandler addNotification;
// Use this for initialization
void Awake () {
onNotification(this, null);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.backgroundColor = Color.green;
GUI.color = Color.red;
GUI.DrawTexture(new Rect(10, 10, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 10, 200 * ((float)objectInteraction.momentum / (float)objectInteraction.momentumMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.green;
GUI.DrawTexture(new Rect(10, 20, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 20, 200 * ((float)objectInteraction.health / (float)objectInteraction.healthMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.blue;
GUI.DrawTexture(new Rect(10, 30, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 30, 200 * ((float)objectInteraction.connections / (float)objectInteraction.connectionsMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.green;
GameObject[] inventory = objectInteraction.inventory;
int currentItem = objectInteraction.inventorySlot;
int y = 100;
for (int i = 0; i < 6; i++)
{
string objName = "";
if (currentItem == i)
{
objName += ">>";
}
objName += ((Object)inventory[i].GetComponent("Object")).name;
if (currentItem == i)
{
objName += "<<";
}
GUI.Label(new Rect(25, y, 250, 50), objName);
y += 25;
}
//GUI.Box(new Rect(Screen.width - 250, 10, 240, 50), "this is atest");
y = 10;
foreach (string s in notifications)
{
GUI.Box(new Rect(Screen.width - 250, y, 240, 50), s);
y += 60;
}
}
public IEnumerable notification()
{
Debug.Log("Notification active");
notifications.Add("test");
yield return new WaitForSeconds(5);
notifications.RemoveAt(0);
}
public void onNotification(object sender, EventArgs e)
{
Debug.Log("this is a test1");
StartCoroutine("notification");
}
}
Answer by Jamora · Jun 23, 2013 at 09:11 AM
To get the coroutine working, change IEnumerable
to IEnumerator
.
Are you sure you need an event system for this? If you have other classes that need to know when a notification has been shown, then go ahead. Otherwise, it's making a simple thing more complicated than it needs to be.
Actually. I don't even need the event system. Having IEnumerable ins$$anonymous$$d of IEnumerator was causing me a lot of confusion as to why everything wasn't working. I can't believe I made that mistake... I have a quick question though.. Is it possible to pass parameters when calling StartRoutine?? $$anonymous$$y guess is no.
Actually I answered my own question. Yes you can, and I understood how everything works to start with. I just made that simple typo.
Your answer
![](https://koobas.hobune.stream/wayback/20220613104714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Keeping track of time? 4 Answers
Rotate GameObject using Coroutine 2 Answers
changing a coroutine's argument at the coroutine's runtime? 1 Answer
how to make a coroutine finish first before other coroutine start 1 Answer