无法在Unity3D中检查脚本中的列表

ki0zmccv  于 2022-11-16  发布在  其他
关注(0)|答案(2)|浏览(124)

我需要检查表来加长,但为什么它不工作。
我想探测目标,我想把摧毁块作为我的飞船
如何制作:
i设置对象列表
如果碰撞者触碰X物体并点击左键-从列表中第一个位置摧毁目标,但如果X物体是X物体-不摧毁列表中的物体
但我得到错误:索引超出范围。必须为非负数且小于集合的大小。参数名称:索引-如果碰撞器接触物体:世界Map,水,浆果
我不能告诉这个细节。最好检查我的代码:

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

public class GetTarget : MonoBehaviour
{
    [Header("Координаты блока")] // block positions
    public int positionX;
    public int positionY;
    public int positionZ;

    [Header("Получение цели")] // set list for block to destroy
    public static List<GameObject> target = new List<GameObject>();

    private void OnTriggerStay(Collider collider)
    {
        target.Insert(0, collider.gameObject);

        if(target[0].name == "WorldMap") // can't destroy world map
        {
            if(target.Count != 0)
            {
                Debug.Log("Вы не можете получить " + target[0]); // you can't get (test message)
                target.Remove(collider.gameObject);  
            }
        }
        if(target[0].name == "Water") // you can't destroy water in structure
        {
            if(target.Count != 0)
            {
                Debug.Log("Вы не можете получить " + target[0]); // you can't get (test message)
                target.Remove(collider.gameObject);  
            }
        }
        if(target[0].name == "Berries") // you can't destroy berries
        {
            if(target.Count != 0)
            {
                Debug.Log("Вы не можете получить " + target[0]); // you can't get (test message)
                target.Remove(collider.gameObject);  
            }
        }
    }

    private void Update() // i get position for collider
    {
        Vector3 positions = transform.position;

        positionX = (int)Mathf.Round(gameObject.transform.position.x);
        positionY = (int)Mathf.Round(gameObject.transform.position.y);
        positionZ = (int)Mathf.Round(gameObject.transform.position.z);

// set position for collider - positions = transfrom.position

        positions.x = positionX;
        positions.y = positionY;
        positions.z = positionZ;
    }
}
mf98qq94

mf98qq941#

虽然我不能肯定没有行号,它看起来像你的错误正在发生,因为你删除的目标,但顶级的if不检查,如果目标计数大于零。
你现在的代码是

if(target[0].name == "WorldMap") // can't destroy world map

您应该更改所有这些if以遵循此逻辑

if(target.Count() > 0 && target[0].name == "WorldMap") // can't destroy world map

这将解决索引超出范围异常。

azpvetkf

azpvetkf2#

回答你的第一个问题,你得到超出范围的原因是,在某些情况下,你删除了列表中的最后一项,但你仍然试图检查target数组[0]索引。你删除了带有target.Remove(collider.gameObject)的元素,这会使后面所有的target[0]检查都超出范围。
代码的快速清理如下所示:

public class GetTarget : MonoBehaviour
{
    // These can be assigned as a Vector3Int.
    [Header("Координаты блока")] // block positions
    //public int positionX;
    //public int positionY;
    //public int positionZ;
    public Vector3Int position;

    [Header("Получение цели")] // set list for block to destroy
    public static List<GameObject> target = new ();

    // You probably only want to check for this when the collider first makes contact.
    private void OnTriggerEnter ( Collider collider )
    {
        /*
            Shouldn't you be checking an object's tag, not the object name?
            e.g.
            if ( collider.CompareTag( "WorldMap" ) ) { ... }
        */
        if ( collider.gameObject.name == "WorldMap" ||
            collider.gameObject.name == "Water" ||
            collider.gameObject.name == "Berries" )
        {
            Debug.Log ( $"Вы не можете получить {collider.gameObject.name}" ); // you can't get (test message)
            return;
        }

        // I don't know why you need to insert at index 0, so I won't change it.
        target.Insert ( 0, collider.gameObject );
    }

    private void Update ( ) // i get position for collider
    {
        var pos = transform.position;
        position = new Vector3Int ( 
            ( int ) Mathf.Round ( pos.x ), 
            ( int ) Mathf.Round ( pos.y ), 
            ( int ) Mathf.Round ( pos.z ) ) ;

        // I THINK you wanted to then assign the rounded position to the transform position:
        transform.position = position;
    }
}

相关问题