Sprite Image Issues during program runtime.

Hello Everyone,
What I am trying to do is simple.
I have a character running around that has the ability to pickup objects.

Upon picking up an object, I want one my blank inventory slots to then have an image of an object.
So far it seems I have the syntax correct:

```csharp
**//declaration of public variables, that I drag and drop from project window:

public Image Slot1; //the first slot in my inventory panel
public Sprite gun; //the image i will use to represent the object I picked up in my inventory**
```

csharp** **Slot1.sprite = gun;** **

Implementing the line above works fine in the Start() function, but anytime I try to Implement this line anywhere else, It does not work. I am getting the error: “Object reference not set to an instance of an object”, whenever I try to implement this line anywhere other than the Start() function.

To summarize, upon picking up an object, I want to change my inventory to have an image of that object in slot 1 of my inventory. This only works when doing this within the Start() function. Like so: (This works only with the Start() function)

How would I go about implementing this outside of the Start() function?

Thanks in advance for any help.

Is this all in one script? Are you accessing Slot1 and/or gun from another script?
How many instances of this component are there in the scene? Are you instantiating any at runtime?
What are the “other places” you have tried to place this code where it does not work?

Can you share more code?

Hello and thanks for the quick response, Sorry i didn’t check back till now as I expected it to take a while for someone to see this.
So, basically I have two scripts, one is attached to the player object, and one is attached to the object that the player picks up.
I am just going to call these two “UserScript” and “ObjectScript”
When the user picks up an object the ObjectScript creates an instance of a class from the UserScript, then calls a function in UserScript which changes slot image to the image of a gun.

The question “Are you accessing Slot1 and/or gun from another script?” got me thinking this might be the reason I am having my issue. I have not tried implementing the image change from one script only. I am thinking that this is the source of error.

I will try implementing my code from one script, and if it does not work I will post again. Thanks a bunch for the help!