So I’ve got this piece of code which manages (or is supposed to I guess) 4 arrays of game objects. The problem is really on all lines of the SpawnAsteroid method but the if statement. All of them throw the same error when separated. I have no idea what could be causing this, especially since I have another method in this very same script that has nothing but the same code as the problematic function (in a slightly different manner) and works without problems:
The problematic code:
public class AsteroidInstatiator : MonoBehaviour {
public int LargestAsteroidArraySize = 2;
public GameObject Asteroid1;
public GameObject Asteroid2;
public GameObject Asteroid3;
public GameObject Asteroid4;
public Vector2 ArrayPosition;
public Rigidbody2D Player;
private Rigidbody2D[] Asteroid1Array;
private Rigidbody2D[] Asteroid2Array;
private Rigidbody2D[] Asteroid3Array;
private Rigidbody2D[] Asteroid4Array;
private int[] iteratorArray;
void Start()
{
iteratorArray = new int[4];
Asteroid1Array = new Rigidbody2D[LargestAsteroidArraySize];
Asteroid2Array = new Rigidbody2D[LargestAsteroidArraySize * 2];
Asteroid3Array = new Rigidbody2D[LargestAsteroidArraySize * 4];
Asteroid4Array = new Rigidbody2D[LargestAsteroidArraySize * 8];
for (int i = 0; i < LargestAsteroidArraySize; i++)
{
Asteroid1Array[i] = GameObject.Instantiate(Asteroid1, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
Asteroid1Array[i].gameObject.SetActive(false);
}
for (int i = 0; i < LargestAsteroidArraySize * 2; i++)
{
Asteroid2Array[i] = GameObject.Instantiate(Asteroid2, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
Asteroid2Array[i].gameObject.SetActive(false);
}
for (int i = 0; i < LargestAsteroidArraySize * 4; i++)
{
Asteroid3Array[i] = GameObject.Instantiate(Asteroid3, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
Asteroid3Array[i].gameObject.SetActive(false);
}
for (int i = 0; i < LargestAsteroidArraySize * 8; i++)
{
Asteroid4Array[i] = GameObject.Instantiate(Asteroid4, ArrayPosition, Quaternion.identity).GetComponent<Rigidbody2D>();
Asteroid4Array[i].gameObject.SetActive(false);
}
}
public void SpawnAsteroid(int asteroidIndex)
{
if (asteroidIndex == 1)
{
Asteroid1Array[iteratorArray[0]].gameObject.SetActive(true);
Asteroid1Array[iteratorArray[0]].position = Vector2.zero;
if (iteratorArray[0] >= LargestAsteroidArraySize * 2) iteratorArray[0] = 0;
else iteratorArray[0]++;
}
}
}
The working code (lower in the same script, thus with the same Start function):
public void OnAsteroidDestroyal(GameObject CurrentAsteroidPrefab, Rigidbody2D CurrentRB)
{
if (CurrentAsteroidPrefab.name == "Asteroid_1(Clone)")
{
Asteroid2Array[iteratorArray[1]].gameObject.SetActive(true);
Asteroid2Array[iteratorArray[1]].position = CurrentRB.GetComponent<Rigidbody2D>().position + new Vector2(-5f, -5f);
Asteroid2Array[iteratorArray[1]].velocity = CurrentRB.GetComponent<Rigidbody2D>().velocity;
if (iteratorArray[1] >= LargestAsteroidArraySize * 2) iteratorArray[1] = 0;
else iteratorArray[1]++;
Asteroid2Array[iteratorArray[1]].gameObject.SetActive(true);
Asteroid2Array[iteratorArray[1]].position = CurrentRB.GetComponent<Rigidbody2D>().position + new Vector2(5f, 5f);
Asteroid2Array[iteratorArray[1]].velocity = CurrentRB.GetComponent<Rigidbody2D>().velocity;
if (iteratorArray[1] >= LargestAsteroidArraySize * 2) iteratorArray[1] = 0;
else iteratorArray[1]++;
}
//repeated four times for four arrays, not bothering you with that
I’ve tried making the arrays public but that doesn’t seem to help. What am I doing wrong?