Item prefab pickup coming back as null

jstoffer

New member
I created an Item pick prefab using the "Item Pickup Templet"

The nature of the item is such that when you use it, it is removed from your inventory, and the game generates yet another version of the exact same item by instantiating yet another one of its item pickup prefabs.

It works at first, but every now and then, the item pickup prefab being instantiated comes back as null in the "item details" of the "item object" script.

What could cause a item pickup prefab to loose the object in it's "item object" script?
 
Do you get any warnings or errors?

I'm assuming you are using some sort of custom Item Action for this?

Is the item Common/Unique Mutable/immutable?

Are you sure the ItemObject loses the reference to the item, or does it get spawned with a null item?

Without knowing what your code does I can't be sure what is going on, but if you do not need your item to be Unique&Mutable I would make it Common&Immutable. That could potentially solve this issue.
 
No, I get no errors.

I added custom code to the "UseItem" Script. I'm using the same "use" command for all items and assigning int values to my items. When an item is used, I check the item value and performing the correct action for the item value.

I just tried all combinations of Common/Unique Mutable/immutable. No change.

I'm not sure if it looses the reference or if something else is happening. It appears that the first time I use the item, it generates the correct item pickup prefab as I made it with no null values. But if I pick that item up and attempt to use it again the next prefab drops with the null values.

Here is the relevant potion of code for the behavior.
 

Attachments

  • Unity.png
    Unity.png
    91.2 KB · Views: 4
Ok so what is happening is that you are pooling the pickup prefab.

When you pickup the first time what happens is that the item is removed from the pickup.
Then the pickup is returned to the pool and no longer has an item.

When you instantiate the pooled object it takes the previous empty pickup and places it it where you want to spawn.

That's why it is empty the second time.


There should be an option on the pickup to "Copy" the item which should avoid removing it from the pickup. If I remember it wrong then another option is to set the item to the itempickup instance in your script.

You can check the DropItemAction source code to see how it is done.
 
Top