Hello everyone.
I'm totally new to Unity and I've followed a tutorial in youtube to undersand who to create a simple level. However, in one of the video (this one if you wanna know : https://www.youtube.com/watch?v=dWtjKKR7Q3s). I've tried to create a moving plateform but unfortunatly the console send me a IndexOutOfRangeException in the Update method.
The code in question is this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaypointFollower : MonoBehaviour
{
[SerializeField] GameObject[] waypoints;
int currentWaypointIndex = 0;
[SerializeField] float speed = 1f;
void Start()
{
Debug.Log(waypoints.Length);
}
void Update()
{
if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].transform.position) < .1f)
{
currentWaypointIndex++;
if (currentWaypointIndex >= waypoints.Length)
{
currentWaypointIndex = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypointIndex].transform.position, speed * Time.deltaTime);
}
}
I've tried to see where is the problem by adding a Debug.Log in the Start function and what is strange is that the waypoints array length seem to be 2 and right after 0 and I don't know how it's possible.
Also the code work fine (the moving plateform work) but I still got that error for some reason.
Can someone help me ?
↧