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 ScorpioGamer · Oct 02, 2013 at 11:31 AM · staticmono-behaviour

Usage of Static Monobehaviour

I was wondering if this is the right way to use them. I am having multiple classes which interact with each other

For Example: PlayerItems interacts with Player using the reference itemManager instead of using GetComponent<> i feel this is much easier to handle. Is there any other way to do this? and is the better than SendMessage? or slower than? public class PlayerItems : MonoBehaviour {

     public static   PlayerItems     itemManager; 
     public         GameObject     head;
     public         GameObject     body;
     
     void Awake () {
         itemManager = this;
     }
     
     public void Equip()
     {
         //Do Something
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jamora · Oct 02, 2013 at 12:51 PM

What you (almost) have there is called the Singleton pattern. An alternative to the singleton is the Monostate pattern (apparently there is no wikipedia page). Either is faster than using SendMessage.

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 ScorpioGamer · Oct 02, 2013 at 01:06 PM 0
Share

Yea well sort of singleton because every scene will have its own version. I'll use singleton with 'DontDestroyOnload(this)' if i wanted only a single version through out the game.

What i was asking is, generally which one is used while development? I've been working on Unity for a while and i hope i am using it right.

avatar image Jamora · Oct 02, 2013 at 01:28 PM 1
Share

You use the same one during development as you do in the released product. Doesn't really matter which one, as long as it works.

avatar image ScorpioGamer · Oct 02, 2013 at 01:37 PM 0
Share

Yea i have worked on some released titles :P i wanted to know if there is any better way of doing it.

Anyways Thanks.

avatar image
0

Answer by citizen_rafiq · Oct 02, 2013 at 02:16 PM

I always avoid to use static member unless i'm not sure member has Class level access. though static member's are 25% faster access then non static member but static member are occupied memory at first before non-static members and they did not release the memories until the application close. if static member reference to image file's then in future when more memory need application will receive memory warning.

In my test result I saw SendMessage() is slower then using GetComponent().action

Comment
Add comment · 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

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

15 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

Related Questions

How to Instantiate objects while in editor? 1 Answer

Static and Coroutines help 2 Answers

Calling non-static from static function C# 2 Answers

Should animated game object be market as a static? 0 Answers

Unity : Singleton ScriptableObjects resets after play 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