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!