What's the best way to reference a list of structs?
I'm doing an inventory system for a game. The items are a struct with some info, and the player inventory is a list with each item (each struct). I want to be able to access the inventory in a Shop, so the player can sell their items.
So I decided to create a script to manage the player (which has the item struct), but for the Shop I decided to create another script (mostly to manage the UI). I want to know what is the best way to reference the player inventory in the Shop script, so it will directly change the player inventory if the player sells an item.
The Shop script is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ManageShop: MonoBehaviour
{
public GameObject player; // player reference
public List<ManagePlayer.Item> items = new List<ManagePlayer.Item>(); // item list reference
void Start()
{
player = GameObject.Find("ManagePlayer"); /* game object has the same name as the script (ManagePlayer). It exists in another scene, so I can't simply add it via inspector */
// gets the item list from the player
items = player.GetComponent<ManagePlayer>().items;
}
}
And this the ManagePlayer script (I decided to put this part only because the full ManagePlayer script is really long)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class ManagePlayer : MonoBehaviour
{
public struct Item
{
public string name;
public Image icon;
public float status1, status2;
public bool isEquip;
}
public List<Item> items = new List<Item>();
}
Please assume I already added some items to the player. Is this the best way to "import" the player inventory into this script? Or does the AddRange
method works better?
Your answer
![](https://koobas.hobune.stream/wayback/20220612195625im_/https://answers.unity.com/themes/thub/images/avi.jpg)