import an array or a list from another script

hello guys.
I am juggling with codes and I am stuck with a situation :
I have my first script GameLogic :

public GameObject[] gameObjects;
    void Start()
    {
        FindPads();
    }

    private void FindPads()
    {
        // check all the instantiated pads on scene with keyword Pads. Pads are instantiated quads from another script on an empty GameObject in the scene
        gameObjects = GameObject.FindGameObjectsWithTag("Pads");
    }

then I have another script on a different empty GameObject called SceneManager ;
=====> case 1 :

    public List<GameObject> resultGOIO = new List<GameObject>();
   
    void Start()
    {
        ImportList();
    }

    private void ImportList()
    {
        for (int i = 0; i < gameObject.GetComponent<GameLogic>().gameObjects.Length; i++)
        {
          resultGOIO.Add(gameObject.GetComponent<GameLogic>().gameObjects[i]);
        }
    }

it seems easy but I can’t make it work.
Unity keeps throwing me NullReferenceException.

I also tried with foreach loop and an array:
=====> case 2

    public GameObject[] resultGOIO;
    public int i = 0;
   
    void Start()
    {
        AnotherImportList();
    }


    private void AnotherImportList()
    {
        foreach (var item in resultGOIO)
        {
            resultGOIO[i] = gameObject.GetComponent<GameLogic>().gameObjects[i];
            if(i <= gameObject.GetComponent<GameLogic>().gameObjects.Length){
            i++;
            }
           
        }
    }

then an array with for loop :
=====> case 3

    public GameObject[] resultGOIO;
   
    void Start()
    {
        AnotherImportList();
    }

    private void AnotherImportList()
    {
        for (int i = 0; i < gameObject.GetComponent<GameLogic>().gameObjects.Length; i++)
        {
            resultGOIO[i] = gameObject.GetComponent<GameLogic>().gameObjects[i];
        }

    }

still nothing.

I thought maybe having those two scripts initiated on Start(), would be troublesome, so I did add a coroutine to gain time but still nothing.

What am I missing?
how can I simply import an array or a List in a new array or List from another script?
thank you for your input!

You say the GameLogic and SceneManager scripts are on different game objects?

In this case, gameObject.GetComponent<GameLogic>() will return null, since that only looks for components on the same game object.

There are a few ways to connect scripts, and which is the right one depends on the use case. You could,

  • Put the scripts on the same game object, then gameObject.GetComponent<GameLogic>() will work
  • Add a public GameLogic gameLogic; field to SceneManager, drag in the game object with the GameLogic script in the inspector and then use gameLogic.gameObjects
  • Turn GameLogic into a singleton, googling “Unity Singleton” should turn up plenty of examples

Also, when you get a NullReferenceException, it’s important to figure out what exactly is null. C# only gives you the line, not what on the line has caused the exception. Use Debug.Log or attach a debugger to check all variables, otherwise you’ll waste a lot of time chasing wrong leads.

Always post the full error including stack trace, this information is crucial to figure out the exact cause.

Ahhhhhhh!
Ok, the famous singleton. I am going to check those out. I watched 3-4 videos, and each time, it was not straight to the point, so it got me confused. Then I went to different solutions.

I will get back to you, if you don’t mind of course, if I have more questions on my problems!
thank you so much!