public void UpdateData(string action, string key, string psr) { bool isItemInDictionary = itemDictionary.ContainsKey(key); if (action == "child_added" || action == "child_changed") { if (isItemInDictionary) { // Set existing data entry ItemData existingItem; bool exists = itemDictionary.TryGetValue(key, out existingItem); // re-add the item again into the tree, as it is not possible to move it directly to another position itemTree.Remove(existingItem); existingItem.CalculatePSR(psr); itemTree.Add(existingItem, existingItem.Position); // set existing item game object, because we want nearby data update to be immediate if (existingItem.DistanceTo(Camera.main.transform) <= visibleDistance) { SetChild(existingItem); } } else { // Add data entry var item = new ItemData(key, psr); itemTree.Add(item, item.Position); itemDictionary.Add(item.Key, item); // create object if it is nearby if (item.DistanceTo(Camera.main.transform) <= visibleDistance) { CreateChild(item); } } } else if (action == "child_removed") { if (isItemInDictionary) { var existingItem = itemDictionary[key]; itemTree.Remove(existingItem); itemDictionary.Remove(key); RemoveChild(existingItem); } } // Start Proximity Check If not yet if (coroutine == null) { coroutine = CoroutineSpawnDespawnChildren(manageGameObjectInterval); StartCoroutine(coroutine); } }