Ohh..Please help me.
I try do Rank System, but have error:
My code:Code:(01:30:24) | [Oxide] 01:30 [Error] Failed to call hook 'OnEntityDeath' on plugin 'drank v0.1.0' (NullReferenceException: Object reference not set to an instance of an object) (01:30:24) | [Oxide] 01:30 [Debug] at Oxide.Plugins.Drank.OnEntityDeath (.BaseCombatEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0 at Oxide.Plugins.Drank.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0 at Oxide.Core.Plugins.Plugin.CallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
If you death by player or suicide - normal. but if you die by Fall/bear/wolf - error...Code:void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { if(entity.ToPlayer()) { BasePlayer player = BasePlayer.FindByID(entity.ToPlayer().userID); if(info.Initiator == player) { UpSuicide(player); return; } if (!info.Initiator.ToPlayer()) { UpDeath(player); return; } else { BasePlayer killer = BasePlayer.FindByID(info.Initiator.ToPlayer().userID); UpDeath(player); UpKill(killer); return; } } }
Solved NullReferenceException with players in OnDeathEntity
Discussion in 'Rust Development' started by Kuhulin, Jul 29, 2016.
-
Wulf Community Admin
Don't assume that the entity is a player, it can be something else too. Set player variables and check if they are null before trying to use them.
-
But i check "if(entity.ToPlayer())" or don't it? I can't understend where i must check...
[DOUBLEPOST=1469836299][/DOUBLEPOST]I don't know how i did it. It's magic for me but it work. Solved.Code:void OnEntityDeath(BaseCombatEntity entity, HitInfo info) { if(entity != null && entity is BasePlayer) { var player = entity as BasePlayer; var killer = info.Initiator as BasePlayer; if (killer == player) { UpSuicide(player); return; } if (killer == null) { UpDeath(player); return; } else { UpDeath(player); UpKill(killer); return; } } } -
Wulf Community Admin
The NRE was with your FindByID, as you were assuming that that the player would be there, but it wasn't and was erroring on the userID as not all entities have a userID. You fixed it by doing away with that and casting to BasePlayer instead, which is fine, or you can keep ToPlayer() and then get the userID from that later if needed, but you don't seem to need it and you didn't need FindByID before either really.
