Getting NullReferenceException in OnTurretTarget
So I have this simple plugin to make autoturret shoot npc. It's working but server console keeps showing red line errors after turret killed npcs. Is there something wrong with my code? Thanks for reply.
        object OnTurretTarget(AutoTurret turret, BaseCombatEntity entity)
        {

            if (entity.ShortPrefabName == "scarecrow")
            {
                return null;
            }
            if (entity.ShortPrefabName == "murderer")
            {
                return null;
            }
            if (entity.ShortPrefabName.Contains("scientist"))
            {
                return null;
            }
            if (entity is BasePlayer)
            {
                return false;
            }
            return null;
        }​

Error is here:

Failed to call hook 'OnTurretTarget' on plugin 'mcTurret v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
  at Oxide.Plugins.mcTurret.OnTurretTarget (AutoTurret turret, BaseCombatEntity entity) [0x00000] in <9fd93d84f51c4a53b599b579a8a90ddf>:0 
  at Oxide.Plugins.mcTurret.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x0005e] in <9fd93d84f51c4a53b599b579a8a90ddf>:0 
  at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <d09a1f46ca2f4432811bcfe45ad13c7b>:0 
  at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <cf88a28c7fb44d36890d85a78331cc9d>:0 
  at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <cf88a28c7fb44d36890d85a78331cc9d>:0  
(Filename: C:\buildslave\unity\build\Runtime/Export/Debug/Debug.bindings.h Line: 35)
I thought maybe problem is on NULL. Not sure.
diaolingtyp
I thought maybe problem is on NULL. Not sure.

If it happens when an entity is killed, it could be that the entity is null, you could check to see if either the turret or entity is null at the start.

   if (entity == null || turret == null)

Otherwise, when you're checking:

entity.ShortPrefabName

It could also be throwing a null exception. So you could probably do:

if (!string.IsNullOrEmpty(entity?.ShortPrefabName) && (entity.ShortPrefabName == "scarecrow" || entity.ShortPrefabName == "murderer" || entity.ShortPrefabName.Contains("scientist")))
return null;
There might be no target so indeed you should check if entity is null.
5ef8090955d35.jpg JamieGB

If it happens when an entity is killed, it could be that the entity is null, you could check to see if either the turret or entity is null at the start.

   if (entity == null || turret == null)

Otherwise, when you're checking:

entity.ShortPrefabName

It could also be throwing a null exception. So you could probably do:

if (!string.IsNullOrEmpty(entity?.ShortPrefabName) && (entity.ShortPrefabName == "scarecrow" || entity.ShortPrefabName == "murderer" || entity.ShortPrefabName.Contains("scientist")))
return null;
Thanks a loooooooooot!!!!!