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 jamespaxi · Jul 24, 2013 at 03:46 PM · c#classstatic

If there is only 1 instance of a class, should it be Static? - C#

Hi All,

I have a "puppetmaster" script with a few "puppet" scripts doing individual pieces of work e.g. collecting game data, setting up the scene with that data, etc...

These Scripts are only instantiated once within the scene and some are utility functions which are likely to be used through the scene.

I'm wondering if I should make these Static, as it would make these global without having to write the following in each individual Script:

 private GameScript gameScript;
 private GameScript2 gameScript2;
 private GameScript3 gameScript3;
 // ...

 void Start(){
   gameScript = gameObject.GetComponent<GameScript>();
   gameScript2 = gameObject.GetComponent<GameScript2>();
   gameScript3 = gameObject.GetComponent<GameScript3>();
   // ..
 }


Any ideas? Thanks! :)

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

Answer by Jamora · Jul 24, 2013 at 04:36 PM

If there must be only one instance of a class then is cannot be static, as static classes can not be instantiated. Instead create a singleton.

However, the vibe I'm getting from your question is that you are in need of a utility class and are asking if it's a good idea. This is a matter of discussion, and better suited for the forums. However, this is what the good people at StackOverflow have to say about the topic.

I think small general utility classes are a good idea, emphasis on small. I usually make utility classes to shorten often used, long, complicated lines of code, like Instantiating a gameobject x distance away from another. I also try to create utility functions using code that rarely changes, like Unity's code, because it makes it harder to break things.

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 jamespaxi · Jul 24, 2013 at 05:08 PM 0
Share

@Jamora Second time you help today! Damn... thanks a ton!

avatar image jamespaxi · Jul 24, 2013 at 06:04 PM 0
Share

@Jamora Thanks. That was a very interesting and helpful read!

avatar image
2

Answer by Owen-Reynolds · Jul 24, 2013 at 06:39 PM

If you want the class to run Update or to start coroutines, it needs to be a regular (non-static) Monobehavior, placed on (probably) an empty.

If it only holds data (lives left, score ...) and maybe some functions to play with that, then it could be static. But, you often want to use the Inspector to set values, drag in textures and sounds... . So still better to have it be a regular class on an empty.

Comment
Add comment · Show 1 · 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 jamespaxi · Jul 24, 2013 at 07:04 PM 0
Share

@Owen thanks this helped a lot!

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

17 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

Related Questions

Accessing Enumeration from another script issue 1 Answer

Multiple Cars not working 1 Answer

Can't get script to access other script/class (c#) 1 Answer

Couroutines not working in static class 1 Answer

Using static constants from an outside script 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