On Start

I’m noob level unity \ c# dev.

In one script(A) I’ve created class for an item with 4 variables.
Class item
string A
int B,C,D

In script (B) I’ve created a public serialized list containing that class List mylist = new List(); (Something like that).
I did this because I want to be able to add a number of these “items” into a list.

I then created a single instance of the item to populate.
item thisitem = new item();
thisitem.A = "sword’
thisitemB = 1
…

then I want to go
mylist.add(thisitem).

However if I do this my list fills up i.e. mylist.count returns the number of items but if I look in the inspector there are no items in the list ?
I expected to see the items in the inspector.

the loop to add the items is in the start of the script B.

Is “item” a serializable class?

If that isn’t the issue, I suggest posting the code using code tags

the “mylist” is serializefield
[serializefield] public List mylist = new List()

the class item in script A has serialized variables, the class is public, public class item : monobeh…

but I definitely don’t have this
[System.Serializable] in the class

Added:
I can see the elements increase in the inspector, but they are blank. That don’t contain any of my items. If I print my item.A, then I can see a value in there that’s the reason why I don’t get why mylist.add(item) creates a new element I can’t see the values.

If you’re still stuck, post your code so we can see what’s going on.

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

public class Data_GarbageBagItem : MonoBehaviour {

    //this is static because I need this list in a order to iterate for cacls.
    [SerializeField] public static string[] garbageTypes = { "nonrecyclable", "metals", "plastics", "rubber", "glass", "chemical", "organic", "paper", "textiles", "electronics" };

    string typename;
    float bagweight = 0;
    float weight = 0;
    float volumne = 0;
    float weightRatio = 0;

    public Data_GarbageBagItem(string TypeName, float BagWeight)
    {
        print("Creating new items called :" + TypeName);
        typename = TypeName;
        bagweight = BagWeight;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Garbage : MonoBehaviour {

    [Header("Bag Info")]
    [SerializeField] float garbageWeight = 10f;
    [SerializeField] List<Data_GarbageBagItem> garbageBag = new List<Data_GarbageBagItem>(); //this bag to fill

    void Start()
    {
        foreach(string newtype in Data_GarbageType.garbageTypes)
        {
            garbageBag.Add(new Data_GarbageBagItem(newtype,garbageWeight));
        }
      
    }

    private void OnCollisionEnter(Collision Pickup)
    {

        Trunk mytrunk = Pickup.gameObject.GetComponent<Trunk>();

        if (Pickup.gameObject.tag == "Pickup")
        {
            mytrunk.SetGarbage(garbageWeight);
            Destroy(gameObject);
        }
    }
}

the bottom bit runs through the array 10 time and display the values. I would have expected to see the 10 array values names in the inspector.

Ok, fixed your code:

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

[System.Serializable]
public class Data_GarbageBagItem
{

    //this is static because I need this list in a order to iterate for cacls.
    [SerializeField] public static string[] garbageTypes = { "nonrecyclable", "metals", "plastics", "rubber", "glass", "chemical", "organic", "paper", "textiles", "electronics" };

    public string typename;
    public float bagweight = 0;
    public float weight = 0;
    public float volumne = 0;
    public float weightRatio = 0;

    public Data_GarbageBagItem(string TypeName, float BagWeight)
    {
        Debug.Log("Creating new items called :" + TypeName);
        typename = TypeName;
        bagweight = BagWeight;
    }
}


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
   
    public class Garbage : MonoBehaviour {
   
    [Header("Bag Info")]
    [SerializeField] float garbageWeight = 10f;
    [SerializeField] List<Data_GarbageBagItem> garbageBag = new List<Data_GarbageBagItem>(); //this bag to fill

    void Start()
    {
        foreach (string newtype in Data_GarbageBagItem.garbageTypes)
        {
            garbageBag.Add(new Data_GarbageBagItem(newtype, garbageWeight));
        }

    }
   
        private void OnCollisionEnter(Collision Pickup)
        {
   
            Trunk mytrunk = Pickup.gameObject.GetComponent<Trunk>();
   
            if (Pickup.gameObject.tag == "Pickup")
            {
                mytrunk.SetGarbage(garbageWeight);
                Destroy(gameObject);
            }
        }
    }

Please note I didn’t do any improvements structure wise, I just fixed it so it shows what you’re expecting.
The issues you were having:

  • Missing [System.Serializable] on class definition
  • Missing public or serialize private definition for Data_GarbageBagItem variables you wanted to see in inspector, hence the reason it displayed empty elements
  • Call for Data_GarbageType seemed to be a typo of Data_GarbageBagItem, not sure if you have that defined outside but I had to change that in order to work.
  • Please note “[SerializeField] public static string[ ] garbageTypes” will never be displayed in inspector as the inspector never shows static variables, so using serialize won’t make a difference.

On a sidenote, this might not be for you now since you’re starting, but this kind of structure can be easily managed by a dictionary. Once you learn a bit more about C# you should have a look a t it.

Excellent thank you.

Modified the response a bit.

Found some good info here on data classes

Solved my problem, thanks