Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by techboysquared1 · Jun 22, 2013 at 09:05 PM · coroutinedelegatesevent-handling

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");
      
     }
     
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image techboysquared1 · Jun 23, 2013 at 09:27 AM 0
Share

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.

avatar image techboysquared1 · Jun 23, 2013 at 09:30 AM 0
Share

Actually I answered my own question. Yes you can, and I understood how everything works to start with. I just made that simple typo.

avatar image techboysquared1 · Jun 23, 2013 at 09:33 AM 0
Share

Thank you for your help!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges