public vs private array in classes
Hi! I am having some confusion about why certain variables have to be public or private. This method spawns certain blocks from this spawner object. It does so by generating numbers relating to a frame number that it should be released on (whether this will work or not I'll find out later). My problem was that when I declared the array spawn times as a public array, the length was always zero and I don't understand why that is.
Secondly, but less importantly, why does the GameObject prefab have to be public? Is it because it is going to create something outside of the class?
public class createBarrier : MonoBehaviour
{
public GameObject prefab;
private Transform spawnLoc;
private int frameNum=0;
private int blockSpawned = 0;
private int[] spawnTimes = new int[10];
// Start is called before the first frame update
void Start()
{
spawnLoc = GetComponent<Transform>();
chooseNumbers(10);
}
// Update is called once per frame
void Update()
{
//print(spawnTimes.Length);
frameNum++;
if (frameNum >= spawnTimes[blockSpawned])
{
Instantiate(prefab, new Vector3(spawnLoc.position.x, spawnLoc.position.y + spawnLoc.position.z), Quaternion.identity);
blockSpawned++;
}
}
public void chooseNumbers(int time)
{
for(int i = 0; i < 10; i++)
{
spawnTimes[i] = (int)Random.Range(0, time * 1000);
//print(spawnTimes[i]);
}
}
}
Thanks for all the help!
Comment