Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Add items to a list

Discussion in 'Scripting' started by Riario93, Dec 4, 2019.

  1. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    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)
    Code (CSharp):
    1.  MyInvento Inventario;
    2.     public GameObject spawnPoint;
    3.     public GameObject[] gemme;
    4.     public Transform[] NewPos;
    5.     public GameObject currentGem;
    6.    
    7.     int index;
    8.    
    9.     void Start()
    10.     {
    11.  
    12.  
    13.        
    14.         index = Random.Range(0, gemme.Length);
    15.         currentGem = gemme[index];
    16.         currentGem.transform.position = spawnPoint.transform.position;
    17.         currentGem.SetActive(true);
    SECOND SCRIPT (STORE IT INTO THE NEW LIST)
    Code (CSharp):
    1. public class MyInvento : provaRandom
    2. {
    3.     #region Singleton
    4.  
    5.     public static MyInvento instance;
    6.  
    7.     void Awake()
    8.     {
    9.         if(instance != null)
    10.         {
    11.             Debug.Log("no good");
    12.         }
    13.  
    14.         instance= this;
    15.    
    16.     }
    17.     #endregion
    18.  
    19.     provaRandom gemma;
    20.     public List<GameObject> MyGem = new List<GameObject>();
    21.  
    22.  
    23.     public void AddItem ()
    24.     {
    25.         MyGem.Add(gemma.currentGem);
    26.  
    27.     }
     
  2. kqmdjc8

    kqmdjc8

    Joined:
    Jan 3, 2019
    Posts:
    102
    You need to declare a public variable at the begining and connect your inventory script just like you connect Game objects. Type following:
    Code (CSharp):
    1. public ScriptName name
    And then you can access public variables from this script by for example
    Code (CSharp):
    1. inventory.currentGem
     
  3. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    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
     

    Attached Files:

  4. Carwashh

    Carwashh

    Joined:
    Jul 28, 2012
    Posts:
    746
    1- What is 'provaRandom' ?
    2- Where does the var gemma get assigned?
    3- Where does AddItem get called?
     
  5. Riario93

    Riario93

    Joined:
    Apr 6, 2019
    Posts:
    21
    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