I have 14 spikes which fall down when player go under them. So when player dies I need to come back spikes to initial position. I have 2 Vector3 arrays: in the first I copy spikes position at the beginning and in the second array there are current positions of spikes.
So I tried to do it through for loop but it raises an error in the for loop in Start
: IndexOutOfRangeException: Index was outside the bounds of the array.
Could you say how can I do it right?
Here is the code:
private GameObject[] spikes;
private Vector3[] spikes_pos;
void Start()
{
spikes = GameObject.FindGameObjectsWithTag("spikes_3");
spikes_pos = new Vector3[spikes.Length];
for (int i = 0; i <= spikes.Length; i++)
{
spikes_pos[i] = spikes[i].transform.position;
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject == bottom || other.gameObject.CompareTag("Spike"))
{
for (int i = 0; i <= spikes.Length; i++)
{
spikes[i].transform.position = spikes_pos[i];
}
}