Add items to a list

Hello everyone,

i am kind of struggling to make this simple code running. I am not a programmer but i am slowly learning while making a 3D game.

The problem should be quite simple to solve hopefully. I will give you a quick briefing following by the code: The game at the start by means of a simple script will select randomly 3 objects from a series of gameobjects i built on the hierarchy. the next step is to put these 3 objects into a List (made by a second script) i will use for other purpose like Re-randomization of the objects to be shown in the map.

here is where I am struggling. I can’t store the 3 objects into a new list. I probably missing something to make the 2 scripts comunicate to each other properly so that the first code will generate the 3 objects and the second Script will take those objects and store them into a list.

FIRST SCRIPT (GENERATE THE OBJECT)

 MyInvento Inventario;
    public GameObject spawnPoint;
    public GameObject[] gemme;
    public Transform[] NewPos;
    public GameObject currentGem;
   
    int index;
   
    void Start()
    {


       
        index = Random.Range(0, gemme.Length);
        currentGem = gemme[index];
        currentGem.transform.position = spawnPoint.transform.position;
        currentGem.SetActive(true);

SECOND SCRIPT (STORE IT INTO THE NEW LIST)

public class MyInvento : provaRandom
{
    #region Singleton

    public static MyInvento instance;
  
    void Awake()
    {
        if(instance != null)
        {
            Debug.Log("no good");
        }

        instance= this;
   
    }
    #endregion

    provaRandom gemma;
    public List<GameObject> MyGem = new List<GameObject>();


    public void AddItem ()
    {
        MyGem.Add(gemma.currentGem);

    }

You need to declare a public variable at the begining and connect your inventory script just like you connect Game objects. Type following:

public ScriptName name

And then you can access public variables from this script by for example

inventory.currentGem

thanks for answer but still I can’t solve the problem.

I have uploaded some pictures hopefully will be more clear.
Basically each series (Serie_1, Serie_2, Serie_3) has the script “provaRandom” which will pick up one object. then the MyInvento Script should take that object and store it into MyGem List.

I was thinking that maybe the fact that provaRandom is on three different series at the same time can be part of the problem so that everytime it try to store the object into the new list it attempt to do it in the first position of the list. so kind of crash it. not sure

5248487–524252–Unity.rar (387 KB)

1- What is ‘provaRandom’ ?
2- Where does the var gemma get assigned?
3- Where does AddItem get called?

Hi,

1- Provarandom is the name of the first script where randomly select objects and move them in specific places with a transform.position
2- gemma.currentGem refer to the object Ive generated in the first script
gameObject [ ] gemme instead are assigned in the inspector
3- I use addItem() as an event in the timeline at the end of an animation.
I ve been calling addItem in the first script but I always get the same error on the console as shown on the pictures I posted