Skip to content
RustPluginGenerator
CS0266Oxide + Carbon

Fix CS0266: Cannot implicitly convert type 'BaseEntity' to 'BasePlayer' (Oxide)

CS0266 means you assigned a value of one type to a variable of an incompatible type without a cast. In Oxide plugins the most common case is storing HitInfo.Initiator (BaseEntity) into a BasePlayer variable; use HitInfo.InitiatorPlayer or `as BasePlayer` instead.

Server console
Error while compiling KillTracker:
[1] Cannot implicitly convert type 'BaseEntity' to 'BasePlayer'. An explicit conversion exists (are you missing a cast?) at 24:33

Why it happens

HitInfo.Initiator is declared as BaseEntity, the base class for every networked entity. A BasePlayer is a BaseEntity, but not every BaseEntity is a BasePlayer (it can be a building block, a dropped item, a turret projectile and so on). C# therefore refuses the implicit assignment and asks you to cast explicitly. The compiler message names the exact line and column, so the error location itself is precise. This typically shows up in damage hooks such as OnEntityDeath, OnPlayerAttack or OnEntityTakeDamage where the attacker is read from info.Initiator. It also appears when someone uses info.Initiator as BasePlayer without a null check afterwards and then calls player-only members, or when a variable is declared as BasePlayer but filled from a different BaseEntity-typed source. The [1] ... at 22:33 tail in the console line is the line:column inside the .cs file.

How to fix it

  1. Open the reported line in the plugin file. Oxide only shows line and column, not the code, so check oxide/logs if you need the full compiler output, or just open the file at that position.
  2. Replace info.Initiator with info.InitiatorPlayer when you only need the attacking player. Oxide sets InitiatorPlayer to null for non-player attackers.
  3. If you want one expression that works for both, use info.Initiator as BasePlayer and then null-check the result before using it.
  4. Never assign info.Initiator to a BasePlayer variable without a cast; if you truly need the BaseEntity, declare the variable as BaseEntity.
  5. Save the file; Oxide recompiles automatically. Confirm in the console that Loaded plugin KillTracker v1.0.0 by RustPluginGenerator prints and no Error while compiling line appears. oxide.reload KillTracker does the same on demand.

Before and after

BeforeFails
using System;
using Oxide.Core;

namespace Oxide.Plugins
{
    [Info("KillTracker", "RustPluginGenerator", "1.0.0")]
    [Description("Prints a message to the killer when a player dies")]
    public class KillTracker : RustPlugin
    {
        private void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
        {
            if (entity == null || info == null)
            {
                return;
            }

            BasePlayer victim = entity as BasePlayer;
            if (victim == null)
            {
                return;
            }

            // CS0266: Initiator is BaseEntity, not BasePlayer
            BasePlayer killer = info.Initiator;
            if (killer == null)
            {
                return;
            }

            if (killer == victim)
            {
                return;
            }

            killer.ChatMessage("You killed " + victim.displayName);
        }
    }
}
AfterFixed
using System;
using Oxide.Core;

namespace Oxide.Plugins
{
    [Info("KillTracker", "RustPluginGenerator", "1.0.0")]
    [Description("Prints a message to the killer when a player dies")]
    public class KillTracker : RustPlugin
    {
        private void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
        {
            if (entity == null || info == null)
            {
                return;
            }

            BasePlayer victim = entity as BasePlayer;
            if (victim == null)
            {
                return;
            }

            BasePlayer killer = info.InitiatorPlayer;
            if (killer == null)
            {
                return;
            }

            if (killer == victim)
            {
                return;
            }

            killer.ChatMessage("You killed " + victim.displayName);
        }
    }
}

FAQ

Why does the compiler say an explicit conversion exists if I cannot convert?

C# sees that a downcast from BaseEntity to BasePlayer is possible at runtime, so it offers an explicit cast. The cast is only safe if the object really is a BasePlayer. as BasePlayer returns null instead of throwing when it is not, which is why the null check matters.

Is InitiatorPlayer always set?

No. For damage dealt by turrets, traps, fire or decay, InitiatorPlayer is null because no player is involved. Always check it before calling player members, otherwise the hook fails with a NullReferenceException in the console.

Does this error come from Oxide or from Carbon?

It comes from the Roslyn compiler in both. Oxide prints Error while compiling <Name>: followed by numbered messages like [1] ... at line:column; Carbon prints the same CS0266 code with its own format. The fix is identical in both.

My code has the cast, but the plugin crashes later. Why?

A cast such as (BasePlayer)info.Initiator throws InvalidCastException at runtime when the initiator is not a player. Prefer info.Initiator as BasePlayer with a null check, which turns a crash into a skipped branch.

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