Skip to content
RustPluginGenerator
CS0236Oxide + Carbon

Fix CS0236: A field initializer cannot reference the non-static field (Oxide)

CS0236 means a field initializer reads another instance member, such as config, before the plugin object exists. Move that assignment into Init (or OnServerInitialized) instead of declaring it on the field.

Server console
Error while compiling ZoneManagerLite:
[1] A field initializer cannot reference the non-static field, method, or property 'ZoneManagerLite.config' at 15:42

Why it happens

In C# a field initializer runs while the object is being constructed, so it can only use static members and constants. When you write something like private List<string> zones = config.Zones; at class scope, the compiler sees a reference to the non-static field config and rejects it with CS0236. Oxide plugins hit this most often when a developer wants a collection, string or number that comes from the config file and declares it directly on a field, assuming the config is already loaded at that point. It is not: Oxide loads the config and calls LoadDefaultConfig/LoadConfig after the object exists. The message always names the member being read (here ZoneManagerLite.config) and the line:column of the field declaration. The plugin is not loaded until this is fixed.

How to fix it

  1. Find the field the error points to (line:column matches the field declaration, not the config field).
  2. Change it to a plain declaration with no initializer, or with a literal default such as new List<string>() that does not touch config.
  3. Assign the config-derived value in Init() or OnServerInitialized() after config has been loaded and after any permission or data setup you depend on.
  4. If the value depends on other plugins (Economics, etc.) use OnServerInitialized() so dependencies are ready first.
  5. Save the file; Oxide reloads automatically. A clean load prints Loaded plugin ZoneManagerLite v1.0.0 by RustPluginGenerator. If the compiling error is gone, the field is now set at the right time. Confirm in game or with oxide.reload ZoneManagerLite that the config value is actually applied.

Before and after

BeforeFails
using System.Collections.Generic;
using Oxide.Core.Plugins;
using Newtonsoft.Json;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("ZoneManagerLite", "RustPluginGenerator", "1.0.0")]
    [Description("Marks configured zones and reports players inside them.")]
    public class ZoneManagerLite : RustPlugin
    {
        private PluginConfig config;

        // CS0236: reads config before the object exists
        private List<string> zoneNames = config.Zones;
        private float reportInterval = config.ReportInterval;

        private class PluginConfig
        {
            [JsonProperty("Zones")]
            public List<string> Zones = new List<string> { "Spawn", "Arena" };

            [JsonProperty("Report interval seconds")]
            public float ReportInterval = 60f;
        }

        protected override void LoadDefaultConfig() => config = new PluginConfig();

        protected override void LoadConfig()
        {
            base.LoadConfig();
            config = Config.ReadObject<PluginConfig>();
            if (config == null)
            {
                PrintWarning("Configuration file is invalid, using defaults");
                LoadDefaultConfig();
            }
            SaveConfig();
        }

        protected override void SaveConfig() => Config.WriteObject(config);
    }
}
AfterFixed
using System.Collections.Generic;
using Oxide.Core.Plugins;
using Newtonsoft.Json;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("ZoneManagerLite", "RustPluginGenerator", "1.0.0")]
    [Description("Marks configured zones and reports players inside them.")]
    public class ZoneManagerLite : RustPlugin
    {
        private PluginConfig config;

        // declared without reading config
        private List<string> zoneNames;
        private float reportInterval;

        private class PluginConfig
        {
            [JsonProperty("Zones")]
            public List<string> Zones = new List<string> { "Spawn", "Arena" };

            [JsonProperty("Report interval seconds")]
            public float ReportInterval = 60f;
        }

        protected override void LoadDefaultConfig() => config = new PluginConfig();

        protected override void LoadConfig()
        {
            base.LoadConfig();
            config = Config.ReadObject<PluginConfig>();
            if (config == null)
            {
                PrintWarning("Configuration file is invalid, using defaults");
                LoadDefaultConfig();
            }
            SaveConfig();
        }

        protected override void SaveConfig() => Config.WriteObject(config);

        private void Init()
        {
            // config is loaded by now, so instance members are safe to read
            zoneNames = config.Zones ?? new List<string>();
            reportInterval = config.ReportInterval;
            Puts($"ZoneManagerLite tracking {zoneNames.Count} zone(s), reporting every {reportInterval}s");
        }
    }
}

FAQ

Why does `private int x = config.Value;` fail but `private int x = 5;` compile?

Literals and constants are known at compile time, so the field can be built during construction. config is an instance field that only exists once the object is created and configured, so the initializer cannot legally read it. Assign it in Init() instead.

Can I use `static` to get around CS0236?

Only if the referenced member is also static and does not depend on per-plugin state. Making the config static usually creates worse problems, since Oxide constructs the plugin per load. Initializing in Init() or OnServerInitialized() is the correct approach.

Is `OnServerInitialized` better than `Init` for this?

Init runs when the plugin is created, before other plugins may be ready. Use Init for permissions and local values, and OnServerInitialized when the value depends on another plugin being loaded or on server entities being present.

The error mentions a method, not a field. Same fix?

Yes. Field initializers cannot call instance methods either. Move the call into Init/OnServerInitialized and store the result in a field declared without an initializer.

Different error?

Paste the full console output and get the cause and a fix in plain English.

Open the error explainer

Skip the debugging

Generated plugins are compiled before you get them, and compiler errors are fixed automatically.

Generate a plugin

2 free plugins a month, no card.

Last updated Oct 26, 2026.

Get plugins that compile first time

Generate