Hi,
I am really new to Unity or programming, so I am just learning. My problem here is that I created a List of Objects which I filled using one method called CreateGrid(). I created another method in the same class called MoveDown() where I intend to use the content of the list mentioned before.
The problem: If I call the method MoveDown() from the function Start() I can access the content of the list previosly created with CreateGrid(). However, if I access the method from another class, the list appears empty.
Question: Can anybody tell me what is happening and a possible solution?
Thank you
Class GM:
using System.Collections.Generic;
using UnityEngine;
public class GM : MonoBehaviour
{
List tileList = new List(); //This is the list
int rows = 7;
int cols = 6;
float x = 0;
float y = 0;
float z = 0;
public GameObject ball;
void Start()
{
CreateGrid();
}
void CreateGrid()
{
for (int i = 0; i < rows; i++)
{
x = 0;
for (int j = 0; j < cols; j++)
{
Vector3 spawnPosition = new Vector3(x, y, z);
Quaternion spawnRotation = Quaternion.identity;
GameObject balls = Instantiate(ball, spawnPosition, spawnRotation);
tileList.Add(balls);
x += 1.2f;
}
y += 1.2f;
}
}
public void MoveDown(float x, float y)
{
for (int i = 0; i < 8; i++)
{
GameObject obj= tileList*;*
print(obj);
}
}
}
Other Class:
using UnityEngine;
public class OnClickDestroy : MonoBehaviour
{
float x;
float y;
public GM gm;
public void OnMouseDown()
{
x = gameObject.transform.position.x;
y = gameObject.transform.position.y;
gm.MoveDown(x, y);
Destroy(gameObject);
}
}