#NullReferenceException: Object reference
1 messages · Page 1 of 1 (latest)
alright
myContainer could be null. myContainer.icon could be null. item could be null
myContainer probably isn't null because you just instantiate a prefab
yeah
Did you assign icon in the prefab?
not just on a random instance of the prefab
on the actual prefab
in my assets
if you double click the prefab asset, it will open in isolation, and you can look at it there
or just click on the asset, since icon will appear in the inspector
oh thats cool
but the icon will have to change every time a new item is in the slot
so do i set an image for the slot?
as default
Image is the component that displays a sprite
ik
And yes, you absolutely have to have one assigned
if icon is null, that code will always throw an NRE
The item container should come with its own Image component
Your code is trying to set the sprite for that image
everything gives me a type mismatch
You're trying to drag a scene object into the prefab.
You can't do that.
This prefab is supposed to be a thing that can display a single item's sprite, correct?
If not, explain how this is supposed to work.
yes it shows a single items sprite
okay, so each item container needs its own Image
im dragging my assets not scene
what are you attempting to drag in, then?
it would not make sense to reference an Image on another prefab
you need to add an Image to the item container prefab
Weren't you originally trying to retrieve an Image from a game object named "icon" on the prefab?
(this wouldn't cause a type mismatch, though)
the icon was from the scriptable object
scriptable objects don't have transforms
i don't understand
myContainer.icon.sprite = item.icon;
Do you mean this line?
I was talking about your original code from earlier.
var itemIcon = obj.transform.Find("icon").GetComponent<Image>();
This bit.
oh
There is room for confusion because you've named two things "icon"
here
You should rename the item container's field
`public class ItemData : ScriptableObject
{
public string id;
public string displayName;
public Sprite icon;
public GameObject prefab;
public bool stackable = true;
public ItemType type;
public enum ItemType
{
weapon,
tool,
material,
consumable,
}
}`
thats the icon the item.icon is
I'd just call it image or iconDisplay
Giving different things (especially things you use together) the same name is a recipe for confusion
i changed it
you might need to re-assign the reference on the prefab now
since the old icon fields is gone
i believe i re assigned it
i still get the same error tho
Screenshot the entire inspector for the item container prefab.
You did not assign it.
i thought thats what was supposed to be null
The Image component will not yet have a sprite assigned to it
so the sprite can be null
but if containerIcon is null, you will get an error when you try to use it.
myContainer.icon.sprite <--- null reference exception immediately occurs
You must drag that Image component into the containerIcon field
so that, after you instantiate the prefab, you can set the sprite of the Image
sorry my wifi decided to commit suicide
anyways i fixed a little up
i realized that itemPrefab should not have been on the inventory slot prefab cause thats the container, instead i put it on an item prefab
which sorta i suppose reveals the purpose of the name
i dont get an error anymore
but it do some funky stuff
it instantiates the prefab, but not actually in the inventory slot, it also randomly creates 45 of them
I thought you wanted to show each item in an item container.
So is there supposed to be just one item container, then?
an item would have an item container?
or do you mean that each item would be in a separate item container?
sounds like you want to instantiate all of the item containers ahead of time and keep them in a list, then
yeah
so you'd have a List<ItemContainer>
i have items addable and removable from the inventory item list
public List<ItemData> itemsInInventory = new List<ItemData>();
item data is the scriptable object
yes, but I'm talking about the actual containers you display your items in
if you're going to create them in advance (maybe making 9 of them for a minecraft-style item bar), you need to keep track of them
hotbar
all the slots are already in the game
so technically nothing should be instantiated container wise
i need to instantiate the item icon into the slots
you don't need to instantiate anything
you just need to assign the icon
I'm having some trouble figuring out your design here
so make a child object of the slot that has a component image
then have the item icon in that slot be what that is
i believe theres one for the button as the child object thats transparent
assign the item's icon into the sprite field of that Image component
for reference to you
alright
if you have a list of item containers, you can do containers[0].containerIcon.sprite = item.icon; to make the first container display whatever item.icon is
i think im gonna come back to this tommorow, ive been working on this for too long and my cranium needs a break
i basically ask you for help on a daily basis
inventory systems require a solid design
yeah
I spent quite a while implementing one for a soulslike game
i left cause i mostly had school work
i think i need some kind of array for my inventory slots
then when adding it'll check if each slot is filled or not
Yes, you should do that.
I thought you were creating and destroying the slots
since you were instantiating itemContainer as you iterated over your items
yeah that's why i changed it to create the itemPrefab
well the itemPrefab sprite is whatever the item data's icon is
how would i do this?
public GameObject reference to each slot would be a lot
if they're all parented to a single object, you could just do GetComponentsInChildren<ItemContainer>() to grab them all