为什么我有ArgumentOutOfRangeException错误?

时间:2020-02-20 14:07:43

标签: c# unity3d

你好,我试图统一倒带时间,但是每次我按住操作按钮时都会出现以下错误:

ArgumentOutOfRangeException:索引超出范围。必须为非负数且小于集合的大小。 参数名称:索引

这是c#代码:

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

public class TimeBody : MonoBehaviour
{

public bool isRewinding = false;
List<Vector3> positions;

void Start()
{
    positions = new List<Vector3>();
}

void Update()
{
    if (Input.GetKey(KeyCode.Mouse0))
        StartRewind();
    if (Input.GetKeyUp(KeyCode.Mouse0))
        StopRewind();
}

private void FixedUpdate()
{
    if (isRewinding)
        Rewind();
    else
        Record();
}

void Rewind()
{
        transform.position = positions[0];
        positions.RemoveAt(0);
}

void Record()
{
    positions.Insert(0, transform.position);
}

void StartRewind()
{
    isRewinding = true;
}

void StopRewind()
{
    isRewinding = false;
}
}

请帮助我不知道错误在哪里

0 个答案:

没有答案
相关问题