Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
1
Question by reincarnationofunity · Jun 27, 2017 at 07:01 PM · staticdesignstatic class

Static class in Unity

For something like an AudioManager or GameManager,there's only ONE AudioManager in the entire game world. So to me, it seems logical design-wise to make a Manager class static. Here is my current AudioManager class.

 public class AudioManager : MonoBehaviour
 {
     public AudioClip[] AudioClips;
 
     private AudioSource _audioSourceComponent;
 
     void Start ()
     {
         _audioSourceComponent = this.GetComponent<AudioSource>();
         PlaySound(0);
     }
 
     public void PlaySound(int clipIndex)
     {
         _audioSourceComponent.clip = AudioClips[clipIndex];
         _audioSourceComponent.Play();
     }
 
     public void PlaySound(int clipIndex, float delayTime)
     {
         _audioSourceComponent.clip = AudioClips[clipIndex];
         _audioSourceComponent.PlayDelayed(delayTime);
     }
 }

If I do not make this class static, I will have to create a public AudioManager and drag and drop this class every time I want a reference to the methods here that will enable me to play audio. It's just a little more work, and it confuses me. I would like to say AudioManager.PlaySound instead of getting a reference to AudioManager, calling it AudioManager (redundant), and then AudioManager.PlaySound.

Does it make sense design-wise to make it static? I know one problem is, when I tried to make everything static, I could not use GetComponent. Can someone explain this to me?

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

Answer by nathanlink169 · Jun 27, 2017 at 10:11 PM

GetComponent works on a specific game object - You can only access GetComponent when a class is attached to a game object: static classes cannot be added to a game object.

There are a lot of design theories about whether static classes are good or bad. The general consensus (from what I've seen) is that they're convenient, yet generally bad practice.

That being said, if you're going to make a class static, managers are the perfect ones to make static.

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
avatar image
1

Answer by Mentoliptus · Jun 28, 2017 at 07:00 AM

You could user the Singleton pattern instead of static classes. The component will still be unique across the whole game, but it will be available in the hierarchy and you can use GetComponent.

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

110 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Static Array JS 0 Answers

Object reference not set to an instance of an object 0 Answers

How to bake lightmap for terrain 0 Answers

Indoor/Outdoor City Level Design 0 Answers

Finding the ideal scale for phyiscs 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