NavMesh errors when spawning entities often
Hi Everyone,

I'm new to Oxide and C# in general. I've managed to cobble together something that compiles but I have an issue I was hoping someone might have some advice for me in order to fix it.

First let me explain why I 'made' this, I run a small PvE server with the idea that its a lower grind etc etc with this in mind I've reduced crate time to something around 5 mins. this typically means players who hack the crate dont even get any scientists spawn and I think that might need some balancing. So I wrote this plugin to spawn scientists when the crate drops to give players something to fight and not make it so easy.

My issue is that when the hook is called and it attempts to spawn the scientist I get the old "cant spawn entity on valid navmesh" error. I suspect that the hook is being called a bit earlier than I'd hoped for and when it gets the crates position its still falling and therefore isnt near a nav mesh yet. I'm thinking I could get it working if perhaps I could "sleep" the command for 5 seconds or something to be sure, the problem is I dont know how to do that.

Any help or constructive critism would be appreciated.

BaronVonFinchus
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using Newtonsoft.Json;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Game.Rust.Cui;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("CrateSpawner", "BaronVonFinchus", "0.0.1")]
    [Description("Spawns Scientists at the hackable crate")]
    class CrateSpawner : RustPlugin
    {
        const string scientistString = "assets/prefabs/npc/scientist/scientist.prefab";
        void OnCrateDropped(HackableLockedCrate crate)
        {
            var newpos = new Vector3();
            var spawnoffset = new Vector3(2, 0, 0);
            var cratepos = new Vector3();
            newpos = crate.transform.position;
            cratepos = newpos + spawnoffset;
            Puts("Crate has been dropped");
            SpawnScientist(scientistString, cratepos);
        }
        private void SpawnScientist(string prefab, Vector3 position)
        {
            BaseEntity scientist = GameManager.server.CreateEntity(prefab, position);
            Puts("Command has been executed");
            if (scientist != null)
            {
                scientist.Spawn();
            }
        }
    }
}​


Does anyone have any suggestions on what I might be doing wrong here or failing that any equivelent to "sleep" for 5 seconds before executing the spawn command?
You can read about timers here:
https://umod.org/documentation/api/timers

However, there's probably a better way of doing it, such as a way to detect when it lands. Not sure how to do that off the top of my head but it's definitely possible one or more ways.
Yeah I do see it could use some refinement, i'll see if I can find out how to do that. Thanks for your help mate, this should at least give me a proof of concept.
You could spawn scientists when the crate hacking has been started.