- Home /
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
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
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.
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
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
Follow this Question
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