Fix CS0165: Use of unassigned local variable (Oxide)
CS0165 means the compiler cannot prove a local variable was assigned before you read it. Initialize the variable when you declare it (for example `BasePlayer closest = null;`) and the plugin compiles.
Error while compiling NearestPlayer: [1] Use of unassigned local variable 'closest' at 51:21
Why it happens
C# requires every local variable to be definitely assigned on every path that reaches a read. Declaring BasePlayer closest; and assigning it only inside a foreach or if block leaves the compiler with one path where the loop body never runs, so the later read of closest is rejected. Oxide compiles your .cs file as a whole, so this stops the plugin from loading even though the logic looks fine at runtime. Typical triggers: a search loop with an early-exit condition, an if (distance < best) assignment where best itself starts unset, a switch branch that never assigns, or a value assigned only inside try. Capturing the variable in a lambda does not satisfy the compiler either, because the lambda might run zero times.
How to fix it
- Find the line and column from the console message (
at 32:20points into the.csfile, not the console). - Give the variable a starting value at the declaration:
nullfor reference types,0/float.MaxValuefor numbers,defaultwhere unsure. - Remove any now-redundant second declaration inside the loop.
- If the value is meant to be filled by a callback or lambda, initialize it before the callback is registered.
- Save the file; Oxide reloads automatically. Confirm with
oxide.reload NearestPlayer— a successful load printsLoaded plugin NearestPlayer v1.0.0 by RustPluginGeneratorand theError while compilingblock is gone. If you still see[1] ... at ..., read the new line:column and repeat.
Before and after
using System.Collections.Generic;
using UnityEngine;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Nearest Player", "RustPluginGenerator", "1.0.0")]
[Description("Reports the closest other player to the caller and teleports an admin to them")]
public class NearestPlayer : RustPlugin
{
private const string PermUse = "nearestplayer.use";
private void Init()
{
permission.RegisterPermission(PermUse, this);
}
[ChatCommand("nearest")]
private void CmdNearest(BasePlayer player, string command, string[] args)
{
if (player == null) return;
if (!permission.UserHasPermission(player.UserIDString, PermUse))
{
SendReply(player, "You do not have permission to use this command.");
return;
}
if (args != null && args.Length > 0 && args[0] == "tp")
{
if (!player.IsAdmin)
{
SendReply(player, "Only admins can teleport.");
return;
}
BasePlayer closest;
float best = float.MaxValue;
foreach (BasePlayer other in BasePlayer.activePlayerList)
{
if (other == null || other == player || !other.IsConnected) continue;
float dist = Vector3.Distance(player.transform.position, other.transform.position);
if (dist < best)
{
best = dist;
closest = other;
}
}
if (closest == null)
{
SendReply(player, "No other players online.");
return;
}
player.Teleport(closest.transform.position);
SendReply(player, "Teleported to " + closest.displayName + ".");
return;
}
BasePlayer nearest;
float nearestDist = float.MaxValue;
foreach (BasePlayer other in BasePlayer.activePlayerList)
{
if (other == null || other == player || !other.IsConnected || other.IsSleeping()) continue;
float dist = Vector3.Distance(player.transform.position, other.transform.position);
if (dist < nearestDist)
{
nearestDist = dist;
nearest = other;
}
}
if (nearest == null)
{
SendReply(player, "Nobody nearby.");
return;
}
SendReply(player, "Nearest: " + nearest.displayName + " at " + Mathf.RoundToInt(nearestDist) + "m.");
}
}
}using UnityEngine;
using Oxide.Core;
namespace Oxide.Plugins
{
[Info("Nearest Player", "RustPluginGenerator", "1.0.0")]
[Description("Reports the closest other player to the caller and teleports an admin to them")]
public class NearestPlayer : RustPlugin
{
private const string PermUse = "nearestplayer.use";
private void Init()
{
permission.RegisterPermission(PermUse, this);
}
[ChatCommand("nearest")]
private void CmdNearest(BasePlayer player, string command, string[] args)
{
if (player == null) return;
if (!permission.UserHasPermission(player.UserIDString, PermUse))
{
SendReply(player, "You do not have permission to use this command.");
return;
}
if (args != null && args.Length > 0 && args[0] == "tp")
{
if (!player.IsAdmin)
{
SendReply(player, "Only admins can teleport.");
return;
}
BasePlayer closest = null;
float best = float.MaxValue;
foreach (BasePlayer other in BasePlayer.activePlayerList)
{
if (other == null || other == player || !other.IsConnected) continue;
float dist = Vector3.Distance(player.transform.position, other.transform.position);
if (dist < best)
{
best = dist;
closest = other;
}
}
if (closest == null)
{
SendReply(player, "No other players online.");
return;
}
player.Teleport(closest.transform.position);
SendReply(player, "Teleported to " + closest.displayName + ".");
return;
}
BasePlayer nearest = null;
float nearestDist = float.MaxValue;
foreach (BasePlayer other in BasePlayer.activePlayerList)
{
if (other == null || other == player || !other.IsConnected || other.IsSleeping()) continue;
float dist = Vector3.Distance(player.transform.position, other.transform.position);
if (dist < nearestDist)
{
nearestDist = dist;
nearest = other;
}
}
if (nearest == null)
{
SendReply(player, "Nobody nearby.");
return;
}
SendReply(player, "Nearest: " + nearest.displayName + " at " + Mathf.RoundToInt(nearestDist) + "m.");
}
}
}FAQ
I assign the variable before the loop, why does CS0165 still appear?
Check that the assignment is on every path. A declaration plus an assignment inside an if (cond) still leaves the else path unassigned. Either initialize at the declaration or add an else branch that sets a value.
Does CS0165 cause a runtime error?
No. The plugin never loads, so no hook runs. Oxide prints Error while compiling <Name>: and keeps the previous version (or nothing) loaded. Fix the compile error first; runtime issues only appear after a successful load.
Why does the same code work in the Carbon console?
Carbon prints compiler diagnostics in its own format, so the line can look different, but the Roslyn rule is identical: an unassigned local is rejected. Apply the same fix.
Is `default` safer than `null` here?
For reference types such as BasePlayer, default is null, so both work. Prefer null for clarity and check for null after the loop before using the result.
Different error?
Paste the full console output and get the cause and a fix in plain English.
Open the error explainerSkip the debugging
Generated plugins are compiled before you get them, and compiler errors are fixed automatically.
Generate a plugin2 free plugins a month, no card.
Last updated Oct 26, 2026.
Get plugins that compile first time
Generate