Help with relatively simple moving platform script..

Hello all! I am new to Unity and working on my first 3d platformer. I’ve had experience in the past with GameMaker and done several 2d projects but this is my first dive into the 3d world =)
I am working on adding moving platforms to my game and was using code from

this video, in the video he demonstrates on a 2d object but says it should work the same with 3d as well, basically the moving platform script is attached to the platform object and children objects are used as the waypoints. The script i have is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingPlatform : MonoBehaviour
{
    public List<Transform> waypoints;
    public float moveSpeed;
    public int target = 0;

    // Update is called once per frame
    void Update()
    {
        //recieve data
        transform.position = Vector3.MoveTowards(transform.position, waypoints[target].position, moveSpeed * Time.deltaTime);
    }

    private void FixedUpdate()
    {
        //use data
        if (transform.position == waypoints[target].position)
        {
            if (target == waypoints.Count - 1)
            {
                target = 0;
            }
            else
            {
                target += 1;
            }
        }
    }
}

and the children are all assigned to the ‘list’, speed is set, and target is left as 0 in the inspector. This mostly works, but its throwing an error in the console every time and it seems to occur every frame as the numbers next to the errors keep increasing the whole time its playing… The error is :

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at <5e2d116f98d140d0a76ec8a673a2a4ac>:0)
MovingPlatform.Update () (at Assets/Scripts/MovingPlatform.cs:15)

However, watching the target value in the inspector it seems to go from 0,1,2, back to 0 the way its supposed to and the platform moves as intended as well… I’m just weary of leaving that bug in there as i feel like it will cause me grief down the road.
Does anybody have any idea what I’m doing wrong here? Or is there a better way to go about this completely? I am using the old input method and playerController… not rigid body, if that makes a difference… Any help would be greatly appreciated, thanks all and have a good day!

If you have an instance of this component in the scene with nothing in its waypoints collection it’ll be throwing this error.

Reason being that even with no entries, it’s going go try and access index 0 every frame. So you should look for said instance and fix that up.

You can also guard update so it doesn’t try to act on an empty list:

private void Update()
{
    if (waypoints.Count == 0) return;

    //other stuff here
}

Or you could have the component shut itself down in Start/Awake. You could even use this to locate the problem component:

private void Start()
{
    if (waypoints.Count == 0)
    {
        Debug.LogWarning($"{name} has no waypoints!", this);
        enabled = false;
    }
}

Also why is the movement happening in Update and the distance check happening in FixedUpdate? They should both be in one or the other depending on whether you’re using rigid bodies or not.

Thank you for the reply, unfortunately adding the first bit of code to the update function docent seem to change things, and adding the start function shuts down the moving platform object which is the only one with the script attached to it… I have tried moving both functions to either the update point or the fixed update point but this just causes the platform to not move anymore, my guess is that they have to happen at slightly different intervals to make the code work… but Maybe there’s a better way to go about this. I like this method because its simple, runs off one code, and all the waypoints are neatly parented inside the platform object, but I am open to doing things a different way if this method is outdated/dosent work on 3d games. I just don’t want to leave that error in there!

I don’t believe that. It should only happen if the waypoints collection’s size is set to zero. Thus, if it’s firing, you have a component somewhere in your scene that has this component with a waypoint size of zero.

Clicking on the Debug.LogWarning as shown should highlight the offending object in your hierarchy. That’s what the second parameter does, it provides ‘context’ as to what is firing off the debug.

This is not true in the slightest either. Update happens every frame, FixedUpdate happens to the beat of your FixedTimestep in your project settings, and this is what the physics system runs to.

FixedUpdate should ONLY be used for physics related matters. If you’re just playing around with simple transforms, Update should be used.

Oh man well this is embarrassing… I was about to contest saying the start function does indeed disable the only object with the script on it, but upon closer inspection I had accidentally placed the same script on the object twice, one with waypoints and one without!! After realizing this ehhh embarrassing mistake and deleting the extra script things are working the way they should now… Thank you for taking the time and helping me realize my error! Have a good day! =)