I have been looking at the different posts in this forum about this problem but I couldn’t find anything that answers my problem. I am new to programming so this error is new to me. Can anyone help me find out what I did wrong with my code? Thanks.
public class move : MonoBehaviour {
public float Speed = 5;
public GameObject randomWall;
public List<GameObject> myWalls;
int curIndex = 0;
void Start()
{
RandomWALLS();
}
void Update()
{
movingWalls();
//Invoke("movingWalls",5);
}
void movingWalls()
{
for (int ctr = 0; ctr <= myWalls.Count; ctr++)
{
if (randomWall == null) RandomWALLS();
float amtToMove = Speed * Time.fixedTime;
randomWall.transform.Translate(Vector3.back * amtToMove, Space.World);
if (randomWall.transform.position.z < -10)
{
GameObject.Destroy(randomWall);
randomWall = null;
RandomWALLS();
if (myWalls.Count > 0)
{
myWalls.RemoveAt(curIndex);
}
}
}
}
void RandomWALLS()
{
curIndex = Random.Range(0, myWalls.Count);
randomWall = myWalls[curIndex];
}
The error points to this part of the code: randomWall = myWalls[curIndex];