Search Unity

Referencing ScriptableObjects by List index or setting the SO as a variable

Discussion in 'Scripting' started by reviyee, Apr 1, 2020.

  1. reviyee

    reviyee

    Joined:
    Apr 4, 2018
    Posts:
    18
    Right now I have a GameManager class which holds all the Ingredients, Quests, Recipes, Skills, etc for my game.
    Code (csharp):
    1.  
    2. public class GameManager{
    3.     public static GameManager instance;
    4.     protected GameManager(){ }
    5.     private void Awake(){
    6.         instance = this;
    7.     }
    8.     [SerializeField] private List<Recipe> _recipes = new List<Recipe>();
    9.     [SerializeField] private List<Ingredient> _ingredients = new List<Ingredient>();
    10.     public Recipe Recipes => _recipes;
    11.     public Ingredient Ingredients => _ingredients;
    12. }
    13.  
    With the lists filled by dragging ScriptableObjects to them.
    For the Recipe ScriptableObject would it be better to have the Ingredients and the Result referenced as:

    Code (csharp):
    1. [SerializeField] private List<Resource> _ingredients;
    or

    Code (csharp):
    1. [SerializeField] private List<int> _ingredients;
    Does one use less memory than the other? Is either faster?
    I already made use of the
    Code (csharp):
    1. List<int>
    to reference the quests already completed by the player character and it works. I'm just not sure if that's the best option or if I should re-do it.

    Appreciate your help.