Hi.
This is my code:
Error:Code:void Loaded() { var containers = UnityEngine.Object.FindObjectsOfType<StorageContainer>(); timer.Repeat(12, 0, () => { foreach (StorageContainer container in containers) { foreach (Item sc in container.inventory.itemList) { if (sc.amount > 55000) { PrintToChat("TEST:"); } } } }); }
The truth is, I don't have any idea what is wrong.Code:Error] Failed to run a 12.00 timer in 'Testing v1.0.0' (NullReferenceException: Object reference not set to an instance of an object)
Thanks!
Getting an NRE with containers and inventory
Discussion in 'Rust Development' started by Reynostrum, Dec 13, 2015.
-
Wulf Community Admin
One of the items you are trying to call doesn't exist, which means it is "null", so you'd need to do some debugging to find out which one is causing it. My guess is that you're either trying to get an inventory that doesn't exist, or something along those lines.
-
Maybe, but if I remove that timer and I put a command like this:
It works.Code:[ChatCommand("lalala")] void estamostesteando() { var containers = UnityEngine.Object.FindObjectsOfType<StorageContainer>(); foreach (BasePlayer player in BasePlayer.activePlayerList.ToList()) { foreach (StorageContainer container in containers) { foreach (Item sc in container.inventory.itemList.ToList()) { if (sc.amount > 50) { // bla bla } } } } } -
Wulf Community Admin
The timer can't really throw an NRE. The code you posted there is a bit different too. Also, why are you trying to do this in Init()? It would be better to handle it in OnServerInitialized(). -
You will have to use FindObjectsOfType inside of the timer because the StorageContainers will be killed and spawned along the time. So your timer hits when one or more doesn't exist anymore.
