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 esitoinatteso · Nov 14, 2015 at 06:54 PM · buildbuild-errorclassstaticfreeze

Static classes and references are bad for Builds... Alternatives?

Hi there! Yeah... that sucks... I've just found out that most of the problems I've been experiencing lately with my builds are related to static classes and references.

Well, static classes most often than not.

Since I've never been taught about " good practices" when coding, before I do something my own way that I'll eventually have to regret, I'd like to hear from you what would be the optimal solution.

Here's my condition:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 [System.Serializable]
 public static class References {
 
     //Static variable to reference the Obstacles layer
     static LayerMask _obstaclesMask;
     public static LayerMask ObstaclesMask{
         get{
 
             _obstaclesMask = 1 << 8; 
 
             return _obstaclesMask;
         }
     }
 }

This Reference class is the one that keeps bringing up problems, it seems its values ( or the whole class itself) is null somehow when inside a build.

That class just holds properties that I reference throughout various other classes.

I need to swap that public static class with something else... maybe a script that inherits from MonoBehaviour that I can attach to a Singleton game object, so I know it's always there and its values are accessible.

But, again, I'd like to hear from you what should be done in this case.

And also, if you could tell me once again why static classes are bad... why is this screwing up?

Thanks :D

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 JoshDangIt · Nov 14, 2015 at 07:46 PM 0
Share

Why are you using [System.Serializable]?

avatar image esitoinatteso JoshDangIt · Nov 14, 2015 at 10:27 PM 0
Share

I erroneously thought I had to serialize it, then found out that I could serialize other stuff to remember what I needed.

I left that there just because, but I'm not perfor$$anonymous$$g serialization on this class :D

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RuinsOfFeyrin · Nov 15, 2015 at 09:05 AM

Hello,

I have a static class that holds references to multiple things as well. I have never had any issues with it, even in actual builds, but there is a caveat.

The trick is to make sure it isn't getting garbage collected... as long as you are accessing this static class frequently enough, it should never get garbage collected.

You are correct however in that the "proper" way to go about this is to use a singleton pattern on a monoBehavior. This will prevent you from having to worry about your static class getting garbage collected.

Hope that helps.

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 esitoinatteso · Nov 15, 2015 at 02:04 PM 0
Share

Thanks a lot buddy! Yes that really helps, I'm about to rewrite the class to implement the singleton pattern.

Though I'm happy to hear that it's possible to make it work as a static class.

I'm not aware of how the garbage collector operates, but I'd like to hear more about how you manage to keep such a class " alive" in the memory... you just make sure to have a call at it once per scene?

Is there a way to tell the garbage collector: " don't manage this script" perhaps?

Thanks for the clarification :D

avatar image RuinsOfFeyrin esitoinatteso · Nov 15, 2015 at 05:55 PM 0
Share

The garbage collector is its own mystical voodoo non-sense so im not entirely sure how it works. I just have a reference in the static class i access often (at least once a frame), and this in turn keeps it alive.

As far as i know there is no way to tell the garbage collector not to collect something, but i could be wrong.

Also if you feel i have answered the original question, please feel free to mark it as so.

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

34 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

Related Questions

Unity freezes when building a project - when Building Resources Folder 3 Answers

Distribute terrain in zones 3 Answers

how to log for a build game freeze error 0 Answers

WindowsPlayer Error when building project 1 Answer

Build failed with errors at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer 0 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