1. Ok this is my code
    Code:
    using UnityEngine;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("Custom Airstrike", "JTC", 0.1)]
        [Description("Custom Airstrike.")]    class CustomAirstrike : RustPlugin
        {           int chktmr = 0;
         
            void OnEntityDeath(BaseCombatEntity entity, HitInfo info)
                {
                if (entity == null || info?.Initiator == null) return;
             
                var victim = entity.ToPlayer();
                var attacker = info.Initiator.ToPlayer();
             
                if (victim.displayName == "Bandit") {
                chktmr = chktmr + 1;
                }
                if (chktmr == 3) {
                ConsoleSystem.Run(ConsoleSystem.Option.Server, "airstrike strike " + attacker.displayName);
                PrintToChat("Airstrike called to " + attacker.displayName");
                chktmr = 0;
             
                }
            }    }
    }
    It compiles fine and works just like I want it to, BUT it keeps going berserk in console with:
    Code:
    at Oxide.Plugins.CustomAirstrike.OnEntityDeath (.BaseCombatEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CustomAirstrike.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.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 hook, System.Object[] args) [0x00000] in <filename unknown>:0
    Can anyone please tell me what's wrong?
     
    Last edited by a moderator: Feb 21, 2018
  2. Wulf

    Wulf Community Admin

    You'd need to provide the full error, not just the bottom part of it. From first glance though, you are assume the entity (victim) is always a player and not doing any sort of null checking on it.
     
  3. Ah I missed one line.

    Code:
    Failed to call hook 'OnEntityDeath' on plugin 'CustomAirstrike v0.1.0' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.CustomAirstrike.OnEntityDeath (.BaseCombatEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CustomAirstrike.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.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 hook, System.Object[] args) [0x00000] in <filename unknown>:0 
     
  4. Wulf

    Wulf Community Admin

    Yeah, your victim is throwing the error because it isn't always a player. You need to add some null and type checking.
     
  5. And how would I do that?
    [DOUBLEPOST=1519232523][/DOUBLEPOST](I'm as new to this as they come)
     
  6. Add checks like:
    if (!(info.Initiator is BasePlayer)) return;
    if (!(entity is BasePlayer)) return;
    Or add single one like
    if (!(info.Initiator is BasePlayer) || !(entity is BasePlayer))
    return;
     
  7. Ok thank you very much.

    But what would I use for if (!(info.Initiator is BasePlayer)) return; in case of NPC such as Scientists? And does entity.ToPlayer(); work in case of NPC or is there another command?
    [DOUBLEPOST=1519301753][/DOUBLEPOST]Would this work?
    Code:
               if (!(entity is BasePlayer)) return;
               if (entity.name.Contains("bear"))
                return;
                if (entity.name.Contains("boar"))
                return;
                if (entity.name.Contains("chicken"))
                return;
                if (entity.name.Contains("horse"))
                return;
                if (entity.name.Contains("stag"))
                return;
                if (entity.name.Contains("wolf"))
                return;
                if (entity.name.Contains("zombie"))
                return;
    
     
    Last edited by a moderator: Feb 22, 2018