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 Sendatsu_Yoshimitsu · Dec 20, 2014 at 12:54 PM · c#event

How do I access a static event in another function?

I have a timer class, which runs my clock, and a MainSimulator class, which does the game logic's heavy lifting. Every time the in-game minute changes, I would like the timer to message the simulator to execute whatever code I've added to the event. In the simulator class, I have:

 public delegate void MinuteTick(); //Everything that executes when the minute changes is added to this
 public static event MinuteTick OnUpdateMinute; //This is the event that the timer needs to call every minute to run the above delegate

To trigger this, in the timer class I have a function that runs on update:

 public void CheckMinutes(float currentMinute){
         if (currentMinute != lastMinute) {
             lastMinute = currentMinute;
             if (MainSimulator.OnUpdateMinute != null)
                 MainSimulator.OnUpdateMinute();   //Message everything subscribed to OnUpdateMinute
         }
 }

However, this produces a pair of errors on the last line and the (blank) line below it, "The name OnUpdateMinute does not exist in the current context", and providing it a component reference to MainSimulator produces a different error, "The event OnUpdateMinute can only appear on the left hand side of += or -=", which is weird since I thought I was using it as the left-hand argument to verify it's not empty before I run it... am I using events completely wrong?

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
0
Best Answer

Answer by .Talon · Dec 20, 2014 at 01:48 PM

You can not execute an event from inside another class. Add an UpdateMinute function in your simulator which calls OnUpdateMinute and call that function in CheckMinutes.

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 Sendatsu_Yoshimitsu · Dec 20, 2014 at 02:21 PM 0
Share

Hmm, that works, thank you. :)

This is likely a silly question, but is there a way to do this that doesn't involve retaining a reference to the simulator class in the timer? I'm trying to keep my program as decoupled as possible, and lots of interdependancy like this makes me a bit nervous.

avatar image .Talon · Dec 20, 2014 at 02:48 PM 0
Share

Well, you have to call the function somewhere and that is either inside the class or from somewhere else. How about merging the two classes into one class that handles all the timer relevant things?

avatar image Sendatsu_Yoshimitsu · Dec 20, 2014 at 02:51 PM 0
Share

That might work, although I'm also hesitant to cram everything into a single pot- what I'm doing for now is simply caching the reference in the timer for performance's sake, I'll probably go back and look at refactoring/cleaning things up once everything is working :)

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

26 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Relationship between Events and Update() 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Multiple Cars not working 1 Answer

How can an editor script know when another script was removed from the project? 1 Answer

Why is this check executing twice? 2 Answers


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