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 Siegewolf · Jan 28, 2019 at 01:57 AM · classstaticmonobehaviourmethodsnon-monobehavior

MonoBehaviour methods in non-MonoBehaviour class

I noticed that in non-MonoBehaviour class, I can still use some of MonoBehaviour methods. So where I wrote in MonoBehaviour class, Instantiate(...), in non-MonoBehaviour class I can update this like of code as MonoBehaviour.Instantiate(...) and it will work perfectly. I haven't found a single problem with this so far. This however applies for static methods of MonoBehaviour class. In case of non-static methods, like StartCorotuine(...), code like MonoBehaviour.StartCorotuine(...) won't work.

Despite this works fine in all my testing so far, I noticed that nobody mentioned code like MonoBehaviour.Instantiate(...) and makes me believe that there is some horrible problem with this, that I am not able to see yet.

I wish to use non-MonoBehaviour classes as library-like classes. Classes full of various utility methods of the same context. Like EffectsTools class, LevelTools class, etc. And exactly in these classes, I never saw a problem with direct use of MonoBehaviour static methods in the way I described.

I also noticed that for example, Instantiate or Destroy can also be coded as GameObject.Instantiate(...) or GameObject.Destroy(...) in non-MonoBehaviour class as well. It also works fine. Perhaps this is better, safer idea?

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

Answer by Pangamini · Jan 29, 2019 at 01:04 PM

Instantiate is a static method of UnityEngine.Object class, from which MonoBehaviour, GameObject and all built-in unity asset types derive. There's absolutely no problem with calling them from anywhere (on the main thread). StartCorotuine is a method of a monoBehaviour instance (it's not static). It can still be called from outside the MonoBehaviour derived class, but on a specific instance, for example

 class MyNonBehaviourClass
 {
    public MonoBehaviour behaviour;
    
    void StartRotine()
    {
       behaviour.StartCoroutine( .... );
    }
 }
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 Bunny83 · Jan 29, 2019 at 03:02 PM 0
Share

Well summarized :). I tend to write too much. This contains all information required.

avatar image Siegewolf · Jan 29, 2019 at 10:23 PM 0
Share

Good to know there's no problem :)

As for StartCoroutine() specifically, this is quite interesting. $$anonymous$$y impression from searching was that it couldn't be really done outside of $$anonymous$$onoBehaviour. I didn't know at all that it could work this way, thanks! :)

avatar image Pangamini Siegewolf · Jan 30, 2019 at 08:10 AM 0
Share

There's no black magic used in Unity. StartCoroutine() is just a public method

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

102 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 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 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 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

Is it possible for a non-MonoBehaviour class to use built-in functions? 1 Answer

Trying to run 5 instances of a static method at once to do an action, probably a dumb thing to do? 0 Answers

Can a static class have an Update() function which Unity calls automatically? 1 Answer

[SOLVED]From superior script to subordinate script 1 Answer

Static class member for offset 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