Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 robinking · Sep 29, 2011 at 11:36 PM · updateeventmono

Is there a way of subscribing to Update() without the script being attached to a GameObject?

I'm only just beginning to learn about events in C#, and I understand that Update() is an event. I want to create a class that I make lots of instances of, which update every frame. There seem to be only two ways of doing that: either instantiate them as prefabs, in which case my hierachy is filled with many gameobjects (which I, being a bit OCD don't like!) - or, call all of their Update() methods from a proper GameObject's Update() method.

But is it possible to derive my class from MonoBehaviour and somehow subscribe manually to the MonoBehaviour.Update() event, so that the instanced objects can update automatically every frame without the script being attached to a prefab?

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

Answer by Bunny83 · Sep 29, 2011 at 11:59 PM

No, the MonoBehaviour-base-class is derived from Component and therefore can only exist on GameObjects. You can't create Components via "new".

In editor-scripts there's the update-delegate you can use, but unfortunately there is nothing similar for the runtime.

You still can derive your class from MonoBehaviour and even attach them to the same GameObject multiple times or just parent the GameObjects to an empty "folder"-GameObject so your hierarchy stays clean. Seperate components are easier to debug since you can simply make things public and view them in the inspector or add a temporary OnGUI to it.

However, i'm a friend of the custom-event-approach. I did a lot with custom interfaces or delegates. That's also the reason why i hate SendMassage :D. Well it could be quite useful in some cases but i simply don't like it. (string-based function calls are a no-go in my eyes... like the string-based GetComponent function they can't be checked by the compiler)

Comment
Add comment · Show 2 · 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 robinking · Sep 30, 2011 at 12:19 AM 0
Share

Thanks - I suspected as much. Ah, but maybe as you suggest I can make a custom event that goes in a GameObject's Update() and so gets fired each frame, and then make each new instance of the custom class subscribe to that? In that way I could create any number of different custom classes that subscribe to my CustomUpdate (or perhaps EveryFrame - I love choosing names!) event, and I don't have to keep writing code blocks to manually update each instance? Am I understanding that correctly?

avatar image robinking · Sep 30, 2011 at 02:44 AM 0
Share

I figured out a great solution using the above method - thanks for the inspiration! I was really pleased with myself. Of course, then I went to add it to the Unify wiki and found an almost identical script with better functionality than $$anonymous$$e!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Calling Update() from Pointer Down not working properly 0 Answers

Is reference better than UnityEvent ? 1 Answer

MonoDevelop broken after updating to Unity 3.5 3 Answers

Wraping input in events 1 Answer

Is there a smarter way to implement event sequencing? 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