#NullReferenceException: Object reference

1 messages · Page 1 of 1 (latest)

coral comet
#

making a thread for this

#

it's impossible to say from the exception alone

wispy dagger
#

alright

coral comet
#

myContainer could be null. myContainer.icon could be null. item could be null

#

myContainer probably isn't null because you just instantiate a prefab

wispy dagger
#

yeah

coral comet
#

Did you assign icon in the prefab?

#

not just on a random instance of the prefab

#

on the actual prefab

wispy dagger
#

in my assets

coral comet
#

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

wispy dagger
#

so do i set an image for the slot?

#

as default

coral comet
#

Image is the component that displays a sprite

wispy dagger
#

ik

coral comet
#

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

wispy dagger
#

everything gives me a type mismatch

coral comet
#

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.

wispy dagger
#

yes it shows a single items sprite

coral comet
#

okay, so each item container needs its own Image

wispy dagger
coral comet
#

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?

coral comet
wispy dagger
coral comet
#

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.

wispy dagger
#

oh

coral comet
#

There is room for confusion because you've named two things "icon"

wispy dagger
#

here

coral comet
#

You should rename the item container's field

wispy dagger
#

`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,

}

}`

wispy dagger
coral comet
#

I'd just call it image or iconDisplay

#

Giving different things (especially things you use together) the same name is a recipe for confusion

wispy dagger
#

i changed it

coral comet
#

you might need to re-assign the reference on the prefab now

#

since the old icon fields is gone

wispy dagger
#

yeah

#

wha now i cant put it in

wispy dagger
#

i still get the same error tho

coral comet
wispy dagger
coral comet
wispy dagger
#

i thought thats what was supposed to be null

coral comet
#

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

wispy dagger
#

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

coral comet
#

I thought you wanted to show each item in an item container.

#

So is there supposed to be just one item container, then?

wispy dagger
#

each item would have their own, or it could be empty

#

dynamic inventory

coral comet
#

an item would have an item container?

#

or do you mean that each item would be in a separate item container?

wispy dagger
#

yeah like their own inventory slot

#

in games like...

#

terraria, minecraft, etc

coral comet
#

sounds like you want to instantiate all of the item containers ahead of time and keep them in a list, then

wispy dagger
#

yeah

coral comet
#

so you'd have a List<ItemContainer>

wispy dagger
#

i have items addable and removable from the inventory item list

#

public List<ItemData> itemsInInventory = new List<ItemData>();

#

item data is the scriptable object

coral comet
#

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

wispy dagger
#

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

coral comet
#

you don't need to instantiate anything

#

you just need to assign the icon

#

I'm having some trouble figuring out your design here

wispy dagger
#

so make a child object of the slot that has a component image

coral comet
#

you already have that, don't you?

#

you already have an Image

wispy dagger
#

then have the item icon in that slot be what that is

wispy dagger
coral comet
#

assign the item's icon into the sprite field of that Image component

wispy dagger
#

for reference to you

coral comet
#

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

wispy dagger
#

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

coral comet
#

inventory systems require a solid design

wispy dagger
#

yeah

coral comet
#

I spent quite a while implementing one for a soulslike game

wispy dagger
#

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

coral comet
#

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

wispy dagger
#

yeah that's why i changed it to create the itemPrefab

#

well the itemPrefab sprite is whatever the item data's icon is

wispy dagger
#

public GameObject reference to each slot would be a lot

coral comet
#

if they're all parented to a single object, you could just do GetComponentsInChildren<ItemContainer>() to grab them all

wispy dagger
#

they're all parented to inventory slots object

#

so all the slots are parented to an object