Problem with instantiating database at runtime

MartinD

New member
Hi! I recently started integrating UIS and UCC together and run into some issues. Basically there is not problem when controller is instantiated in static way (put controller prefab into scene and do nothing else). However due to the fact that game is supposed to have several km2 overland and that it is using UMA I cannot work this way in the actual product and controller prefab must be instantiated at runtime for instance to avoid loading unnecessary game objects and terrains.

However when I try to do that I run into problem with instantiating database as all references are being removed for some reason. What is the way to fix it (in player)?
prefab.pnginstantiated_object.png
 
Now I also noticed that I should ask it in Questions but sadly cannot move thread there so please, help with that as well:)
 
So the InventorySystemManager is a singleton, it should be created at the start of the game (before any dependencies, i.e Inventories and UI) and never removed.
If you have a bootstrap/initialization scene or if you have a game manager prefab with all your singletons inside all scenes that's where you would put it along side all the Opsive Managers and singletons.

Secondly, during edit mode, the Inspectors that use the database either get the current data base from the InventorySystemManager within the scene or if it does not exist, it will get it from the Inventory Editor window.
Here is an empty scene with no InventorySystemManager where I added an Inventory component and it automatically found the database within the Editor
1646125511475.png
I hope that helps
 
Ok, so here is the content of Game object. As I said if I put player character controller prefab directly in the scene and instantiate is this way it works. However if controller is instantiated at runtime then it for some reason cannot load the database. In either case those managers are present as this is also part of the object that triggers loading and instantiating controller prefab.

So this is how it works in detail:
  1. Scene contains object Game that has all Opsive managers - it is added in edit mode. Nothing happens here at runtime.
  2. Prefab containing Game object also contains Player Loader object which is responsible for instantiating player character controller. This object in hierarchy is put beneath Game and instantiating is done within Start method so it this code should be triggered after all Game object components are processed
  3. Player Loader instantiated player controller in designated area - this is done with all components within Game object intact. Inventory component is within controller object as this is what I ended up after running those UCC and UIS integration steps (component was not added by me).
Based on what you write it should work however for some reason it doesn't.
  1. Game_object.png
 
Last edited:
That's odd.
I'm assuming you are getting errors/warnings when the player is instantiated. Perhaps something to do with execution order.
If you have any errors/warnings I would recommend you try to fix them one by one starting from the first one that happen.
Do post the full error/warning logs here.

Something else you can do is make a quick video of your setup and show how it works, since that could give me a better idea of what is going on.
And if nothing else works you can either send us your project so that we can have a look. Or better even, recreate the same problem using a new project with just the minimum to replicate the issue.
 
No errors and no warnings sadly. This is why I am curious what is going on as based on all info I get it should just work. In the meantime I have found some possible workaround but not sure how much of the solution it could be in a long term. I have also found that even though in inspector for InventorySystemManager there is some database referenced when InventorySystemManager.Awake() is run m_Database is still null for some reason. What I have done as a workaround:

I have moved whole inventory to Resources folder and added following code to be executed before player controller is instantiated:

C#:
string dbPath = "Inventory/InventoryDatabase/InventoryDatabase";
InventorySystemDatabase database = (InventorySystemDatabase)Resources.Load(dbPath);
InventorySystemManager.AddDatabase(database);

It seems to do the job since now I can finally pickup an item and add it to inventory.
 
Interesting. Normally the InventorySystemManager should have the Database set in the serialized field directly. Not sure why it would become null.
Perhaps Unity is acting up. Try removing the reference to the database and resetting it in the InventorySystemManager Inspector then save the prefab or scene.
 
Top