Skip to content

Static Access

The Singletons package comes with a powerful tool for accessing instanced members of a Singleton class without the Instance field. This is done by tagging the members with a [StaticAccess] attribute.


using Hibzz.Singletons;

public partial class PrefabManager : Singleton<PrefabManager>
    [StaticAccess] private GameObject p_enemyPrefab;

Because of the StaticAccess attribute, the property can be publicly accessed without the need for the Instance field.

var enemy = PrefabManager.EnemyPrefab;

It’s very important to note that the class PrefabManager is a partial class. This is very important for this functionality to work. Explanation is available in the “How does it work?” section.

Supported Member Types

Currently, the [StaticAccess] attribute can be applied to the following member types:

  • Fields
  • Properties
  • Methods

Naming the Members

By default, the name of the member is automatically generated by removing anything before the first underscore and capitalizing the first letter of the remaining name. For example, the member _enemyPrefab would be named EnemyPrefab when made a static accessor.

Alternatively, you can specify the expected name of the member by passing it as a parameter to the attribute.

[StaticAccess("CustomName")] private int some_Memeber;

// Accessing the member
var value = CustomSingletonClass.CustomName;

How does it work?

This functionality is achieved using dynamic code generation. The code is generated as a partial class, so it can be merged with the original class. This is why your singleton class must be a partial class for this functionality to work.

The generated code is placed in a file called scriptname.Generated.cs and is placed in the same folder as the original class. This file is automatically generated and should not be edited manually as any changes will be overwritten and lost when the code is regenerated.

The code is generated whenever the contents of the singletons class using the [StaticAccess] attribute is changed. Whenever the all [StaticAccess] attributes are removed from the class, the generated code is deleted. However, if you delete the script manually, make sure to delete the generated code as well.

Disabling the functionality

If you found the frequent code generation annoying when editing the class and none of the members were changed, you can temporarily disable the functionality by toggling the “Pause Automatic Code Generation” option under the “Hibzz > Singletons” menu.

Alternatively you can completely disable the functionality by adding the following flag to the Scripting Define Symbols in the player settings:


Also, if you want to have a more streamlined approach to managing Scripting Define Symbols, you can use the Hibzz.DefineManager. The Singletons package has a neat integration with it, so you can easily toggle the define symbols from the Define Manager window.