UnitReaction can return nil. Here's a version based on the coloring in my oUF layout that will do class colors for players, or gray for tapped units, or reaction colors for other units, with a fallback in case of invalid class/reaction data:
Lua Code:
hooksecurefunc("UnitFrame_Update", function(self)
if not self.name then return end
local color
if UnitIsPlayer(unit) then
local _, class = UnitClass(unit)
color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class]
elseif UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then
color = GRAY_FONT_COLOR
elseif UnitIsEnemy(unit, "player") then
color = FACTION_BAR_COLORS[1]
else
local reaction = UnitReaction(unit, "player")
color = reaction and FACTION_BAR_COLORS[reaction] or FACTION_BAR_COLORS[5]
end
if not color then
color = NORMAL_FONT_COLOR
end
self.name:SetTextColor(color.r, color.g, color.b)
end)