Help: items not getting added to list.

Hey guys,

I’ll just get straight to the point. I have an itemManager with two lists. One list contains all of the resources in the game, the other contains all the resources that the player currently has. Both lists should contain scriptableObjects, although only the resourceList has resources while the currentResourceList is empty.

When the player presses a button, AddResource() is called by another script. this method chooses a random resource from the list and returns it for another script to pick up and instantiate. AddResource() is also supposed to add the resource to the list of current resources but it doesn’t. That’s the problem I can’t find a solution for.

The method returns a resource perfectly, but I can’t get the resource to be added to the currentResources List.

Any help would be greatly appreciated.

thanks in advance,

Lajos

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ItemManager : MonoBehaviour
{
    public List<Resource> resourceList;
    public List<Resource> playerResourceList = new List<Resource>();

    private static ItemManager instance;
    
    private void Awake()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Destroy(gameObject);
        }
    }
    
    public Resource AddResource()
    {
        Resource resourceToAdd = resourceList[Random.Range(0, resourceList.Count)];
        playerResourceList.Add(resourceToAdd);
        return resourceToAdd;
    }
}

just a quick update, i noticed the items getting added to the list but on my ItemManager prefab, not on the ItemManager in my scene.

i’m not sure what to think of this o.O

private static ItemManager instance;

You are creating a half baked singleton. You need to address the instance member through the class i.e. ItemManager.instance but you haven’t made access to it public so its pretty unusable.