→ WoW API
Returns the name and realm of the specified unit.
name, realm = UnitName("unit")
- String - The UnitId to query (e.g. "player", "party2", "pet", "target" etc.)
- String - The name of the specified unit, "nil" if not applicable (e.g., if the specified unit is "target" but the player has no target selected).
- String - The realm the specified unit is from, e.g. "ChamberofAspects", if different from the player's own realm. If the unit and the player stem from the same realm, this value is
The following snippet prints the player character's name to the default chat frame:
local playerName = UnitName("player") print(playerName)
- UnitName("unit") (or any other unit) will return "Unknown Entity" (Actually the value of the UNKNOWNOBJECT global) if called before the unit in question has been fully loaded into the world.
- The realm return will be nil even if the player is from a different realm, if it is out of visible range.
- Realm name will not include server type.
- Note that this function always returns two values even though the latter may be nil. The difference is usually moot, but there are some cases where it matters, e.g.
tinsert(myTable, UnitName("player")), which would previously work, but will now cause errors. The fix is to put the expression in parenthesis:
tinsert(myTable, (UnitName("player")) ).
- The default Blizzard UI defines GetUnitName("unit", showServerName) which only returns the unit name, but for characters from another server appends the server name (showServerName==true) or "(*)" (if showServerName==false).
- The return values will change if the unit in question is under the effects of (see Wowhead comments) or a similar effect. Consider using a different API if feasible for your addon, such as GetRaidRosterInfo, which does not alter the unit's name.