• 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 kannan21 · Sep 16, 2014 at 07:57 AM · eventinheritancemonobehaviourcustomsendmessage

How to call a function on all scripts which are derived from monobehaviour on awake and ondisable?

I need to call a certain function on all scripts w$$anonymous$$ch are inherited from monobehaviour on awake and ondisable. How to do that?

For example if i need to print the name of the gameobjects w$$anonymous$$ch has the monobehaviour script on awake and ondisable how to do that?

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

Answer by Baste · Sep 16, 2014 at 12:48 PM

I can't quite see why you'd want t$$anonymous$$s, but if you insist, I can see two ways:

1: create a MyMonoBehaviour class that does what you want on Awake() and OnDisable(). Have every other script inherit from MyMonoBehaviour instead of MonoBehaviour.

The Awake and OnDisable methods needs to be protected virtual, as your other scripts has to override them (and call base.Awake and base.OnDisable) to be able to define their own behaviour for the methods

2: Make a crazy controller that on every frame:

  • picks up all gameObjects in the scene with FindObjectsOfType()

  • figures out w$$anonymous$$ch ones are new, add them to a list and call the Awake-stuff on them

  • if anyt$$anonymous$$ng in the list was enabled last frame but is now disabled, call the OnDisable stuff on it

  • if anyt$$anonymous$$ng in the list is destroyed, remove it from the list

The second method is harder, especially when it comes to keeping track of what status stuff had on the last frame. It will also eat more resources; every frame you add an O(n) operation, where n is the number of scripts in the game. So I'd recommend method 1. Or do what a sane person would do, and add debug messages where you need them instead of EVERYWHERE.

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 Kiwasi · Sep 17, 2014 at 07:09 AM 0
Share

This has enough detail to justify being an answer.

Another way would be to write a custom wrapper for Instantiate that registers the newly created GameObject with a controller class. That's a cheaper method then checking every frame.

avatar image kannan21 · Sep 17, 2014 at 07:24 AM 0
Share

I just said that i want to print names of all objects for an example. That is not what i intend to do. What i am actually doing is i created an atrribute called save and it will retive the value from playerprefs of the values marked with that attribute on awake and save it on disable. I was just wondering if there is anyother way other than inheriance. Looks like i just don't want to complicate things any way thank you so much for your input.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

How do I call function in inhereted script? 1 Answer

MonoBehaviour Inheritance, Singletons and Generics 1 Answer

What is the most elegant solution to replace multiple inheritance for this? 0 Answers

Issues Inheriting classes and monoBehaviour 1 Answer

Serializable class with coroutines? 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