Need help using Raycast to Pickup items

Mike1972

New member
We have been reading the other post about setting up raycast for pickup, can't really see everything that was done but most of it. Here is what we got so far, I want to be able to only raycast when zoom, which is working fine, it seems that it's not able to pick up or add to the inventory.Can someone help me out with this script, thanks.
 

Attachments

  • RayCastPickup.cs
    1.9 KB · Views: 15
  • error 1.PNG
    error 1.PNG
    125.1 KB · Views: 9
  • error 2.PNG
    error 2.PNG
    129.9 KB · Views: 10
I don't see anything obviously wrong with your custom Raycast Pickup script.

The first error might be causing the second one.

My guess is that you are referencing a null object in the ItemCollectionRestrictionObjects Array:
1625555349347.png

I will change the code such that the error is replaced by a warning. Hopefully that helps others.

If that's not it, could you send me a screenshot of the function with the line causing you the error "Inventory line 89"
 
I have checked everything I know, it is probably something so simple I am overlooking it, this is the line it stops at.

41 _itemCollection.AddItem(info);
 
Looking at the screen shots you sent me, the line you pasted above is the root of the second error.
But that does not help me find what your issue is.

When there is an error, you should always solve them in order, Top first.
When I said to show me your code I meant showing me the Inventory class, the Initialize function around line 89, where the error is called. I'm asking that because my Inventory script might be different from yours, since I do not know which version you are using.

Hopefully we'll get to the bottom of this soon
 
I am not the coder of the team, lol so when you say ( I meant showing me the Inventory class, the Initialize function around line 89, ) I am thinking you want the inventory.cs from my project, if not please explain.
 

Attachments

  • IInventory.cs
    6.3 KB · Views: 3
Ok, so the reason for the first error was that there was an inventory script attached to the item, and I was reading the other post about Raycast and you said that the inventory script was not needed on the object if I was Raycasting. So removed the Inventory Component from the object we want to pick up and now the 1st error is gone. Now only getting the 2nd error when I push the F key to pick up. I have attached a screen shot of the error and the Item I want to pick up, maybe I dont have something right on the item.
 

Attachments

  • Pickup ItemMushroom.PNG
    Pickup ItemMushroom.PNG
    108.8 KB · Views: 5
  • errorIS.PNG
    errorIS.PNG
    46.7 KB · Views: 4
Last edited:
I am thinking you want the inventory.cs
Yes that's exactly it. But you just sent the IInventory (notice the "I" in front of inventory), that's the inventory interface, not the actual inventory. Please send me your actual Inventory.cs script.

Knowing that the error came from the Inventory on the Item might help me find out the first error.

In the other post I mentioned that an ItemPickup can either be an ItemObject (if you want to pickup one item) or an Inventory (if you want to pickup multiple items).

In this case you are using an ItemObject to pickup one item.

Could you also send me your ItemCollection script line 394 which is giving you the error does not point to anything in my project.

Also send me a screen shot of your character Inventory inspector. Is there any chance that your character gameobject is disabled which prevents the Inventory from initializing?
 
I also added 2 pick up objects as you have in the Integration video , for the same mushroom and I can pick those up. If you need anything else let me know, thanks for your help and time,
 

Attachments

  • Inventory.cs
    39.3 KB · Views: 2
  • ItemCollection.cs
    49.8 KB · Views: 5
  • Character Inventory.PNG
    Character Inventory.PNG
    238 KB · Views: 7
Thank you.

So the Inventory bug was what I expected, it's the thing I mentioned in the first reply.

The second error with the ItemCollection happens on line 394 meaning the m_ItemStack variable is null. This can only happen if the ItemCollection wasn't initialized. But that does not make sense since you say that you can pickup items with the normal pickup method.

So my only guess is that the player component you are referencing on the raycast pickup isn't the Main Player. Is that possible?

In your Raycast script you get the player by tag, is there any chance there is another player character somewhere in the scene with an uninitialized Inventory?

just to be sure add a Debug.Log on the player game object you are getting using the Player tag. Also add one on the Inventory and the ItemCollection you are getting. That should confirm whether or not you are getting the correct character.
The next thing to try is adding the item to the Inventory instead of the itemCollection. That shouldn't change anything since adding an item to the inventory adds it to the main itemCollection under the hood, but it is worth a try.

Another potential cause of this problem might be the "Domain Reload" options. Have you changed your Unity Editor options to quickly enter playmode? It's possible that the ItemCollection thinks it is intialized when it is not which causes m_ItemStack to be nulll. But to be fair that doesn't make much sense since you can still pickup items using the normal method

I hope this helps
 
Top