- Home /
Why can't it find the item in game, but knows what it is in script?
I am creating a shop for my game and I have become stuck on one part. I am creating a function called BuyItem. When I click on the buy button it should Debug.Log the message. When I click 'item' in the script it highlights all of the others that are the same thing. But when I play the game and click the button, it says "NullReferenceException: Object reference not set to an instance of an object" but the rest of the code can find the item.name. I have tried putting a parameter of the item type in the () but then the buy button does nothing. What am I missing?
Answer by ignacevau · Apr 29, 2018 at 09:14 AM
I highly doubt that at the moment you get that error, the other parts of your code are able to access the item object. When you get NullReferenceException, it means there is simply no instance set to your object. Possibly you forgot to assign it in the editor, supposing it is a public variable?
That's what I thought as well. The item comes from a list of scriptable objects.
The discDisplay call at the top of the picture works fine. It takes the items information such as name and description and displays it in UI for me. But using that same item.name call as seen in the BuyItem function calls an error.
This picture is showing it in runtime.The '0', '1' squares on the left are instantiated items and the right is the items details that the script above fills out. Every time I click on one the items the details on the right is updated and a Debug.Log tells me what item I have clicked. But as soon as I hit buy I get the error.
With the current amount of information given, it is hard to find the cause. $$anonymous$$ake sure that you don't mess with the item object between displaying the item information and calling the BuyItem method. Also, if you haven't tried it already, try restarting Unity.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Dynamic update List<> 1 Answer
How to call the same functions both in FixedUpdate and LateUpdate? 1 Answer
Help me finish my room generator 0 Answers
How to make GUI Button a fixed size? 1 Answer