Reputation: 109
So I have a user script that contains a List inventory. Then I use a public static User user to access a single user's information throughout the game.
The problem is at some point I'm losing the link to that inventory. In the awake function if I call Debug.Log(User.user.inventory.Count), it prints 0. however if i try and access this inventory at any future time I get a Null Reference error as if it doesn't exist and can't add to it.
If I make the size of the list in the inspector to 1, then it somehow exists and I can add things to it forever, however then I have a dead spot at index 0.
private void AddItemToInventory(ItemObject item, Image uiSprite)
{
User.user.inventory.Add(item);
uiSprite.sprite = GenerateOrbRaritySprite(RaritySprites.raritySprites, item.rarity);
uiSprite.enabled = true;
}
It is failing on the inventory lookup, even though I can see it in the inspector fine.
My User has the following Awake() function so that it stays between scenes:
void Awake()
{
if(user == null)
{
user = this;
DontDestroyOnLoad(gameObject);
}
else if (user != this)
{
Destroy(gameObject);
}
}
Any ideas? Thank you!
Upvotes: 1
Views: 34
Reputation: 260
Try with just one User.
private void AddItemToInventory(ItemObject item, Image uiSprite)
{
user.inventory.add(item);
uiSprite.sprite = GenerateOrbRaritySprite(RaritySprites.raritySprites, item.rarity);
uiSprite.enabled = true;
}
And since this method doesn't have access to the user the user will need passed in as a parameter.
private void AddItemToInventory(User user, ItemObject item, Image uiSprite)
{
user.inventory.add(item);
uiSprite.sprite = GenerateOrbRaritySprite(RaritySprites.raritySprites, item.rarity);
uiSprite.enabled = true;
}
Upvotes: 1