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 SlimeEnthusiast · Feb 12, 2017 at 05:51 PM · static

Am I "misusing" the concept of statics with this implementation?

In each of the scenes of my game there are a number of "Module" objects. Certain operations require a list of all Modules in the current scene. I dislike using some sort of "ModuleHolder" class to hold a reference to all Modules because this needlessly splits Module-related code across two separate classes. My solution has been to find all objects of type Module when the scene loads and store this in a static array in the Module class itself.

This works swimmingly with one caveat. As different scenes have different modules (and even reloading the same scene creates different instances of the same Modules) I need to reset the array holding all Modules on any scene change. This leaves me with a nagging feeling that I am using statics "incorrectly" in some way.

The fact that my current implementation just works may be paramount, but I was wondering if there was perhaps an alternative way to do this that I might be missing. Any thoughts (or reassurances that I am not, in fact, abusing the static property)?

Comment
Add comment · Show 2
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 Bonfire-Boy · Feb 12, 2017 at 06:19 PM 1
Share

$$anonymous$$y thought is that you should stop worrying about "needlessly splitting related code across two separate classes". Having a type and then a manager of some kind that manages objects of that type is a common and useful idiom. It's not "needless", it's powerful and flexible.

avatar image CarterG81 Bonfire-Boy · Feb 15, 2017 at 06:41 PM 0
Share

100% agree with @Bonfire-Boy here.

When i read the OP say

I dislike using some sort of "$$anonymous$$oduleHolder" class to hold a reference to all $$anonymous$$odules because this needlessly splits $$anonymous$$odule-related code across two separate classes.

I facepalmed hard.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RobAnthem · Feb 12, 2017 at 06:50 PM

I prefer statics over singletons, because a singleton has other things going on off the bat, and there is an incredibly simple method of making your statics work how you want for swapping scene data. This is what I personally do.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 namespace myExample
 {
     public static class ExampleManager
     {
         public static exampleScripts[] MyScripts
         {
             get
             {
                 if (myScripts== null)
                 {
                     MyScripts = GameObject.FindObjectsOfType<exampleScripts>();
                 }
                 return myScripts;
             }
             private set
             {
                 myScripts = value;
             }
         }
         private static exampleScripts[] myScripts;
     }
 }

When you switch scenes, any scripts in that scene are of course no longer being referenced, so you make your static file never return null by auto-filling the reference with whatever you intended.

Comment
Add comment · Show 13 · 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 SlimeEnthusiast · Feb 12, 2017 at 06:54 PM 0
Share

This is precisely what I'm doing, however I am not using a handler but rather storing the static array of $$anonymous$$yScripts in exampleScript itself (using your notation).

avatar image RobAnthem SlimeEnthusiast · Feb 12, 2017 at 07:10 PM 0
Share

Hmm, could I see a code example?

avatar image Bonfire-Boy RobAnthem · Feb 12, 2017 at 08:09 PM 1
Share

@SlimeEnthusiast You're right about them sharing the single static array. But it is inefficient because every instance will reinitialise the array using Find. Ins$$anonymous$$d, they could each just add themselves to the array (in this case one would probably facilitate that by using a List ins$$anonymous$$d of an array). This would improve efficiency since you wouldn't need to use Find at all.

You could also use OnDestroy to remove them from the list as and when they are destroyed individually, which could sort out your whole scene-change issue.

In other words something based on this...

 private static List <ExampleScript> allExampleScripts = null;
       
 void Awake()
 {
     if(allExampleScripts == null)
     {
         allExampleScripts = new List< ExampleScript>();
     }
     allExampleScripts.Add(this);
 }
 
 void OnDestroy()
 {
     allExampleScripts.Remove(this);
 }

With regard to the singleton manager approach, I use managers of the sort I described all the time, and have done for over 30 years with no serious problems. Like I said, there are gotchas, but there always are when you use static data. With a singleton manager class, once you've got a pattern for the singleton itself that works (which isn't hard in Unity I$$anonymous$$O) you can forget all that. The only static field you have is the singleton instance, the functionality of which is totally hidden in the singleton class.

Show more comments

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

getting static vars from other scenes? 2 Answers

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

Create an Instance? 2 Answers

Global variables - static keyword ? UnityScript? javascript? 1 Answer

static function? 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