【问题标题】:Index was outside the bounds of the array. 3d array [duplicate]指数数组的边界之外。 3d 数组 [重复]
【发布时间】:2021-08-12 06:11:11
【问题描述】:

IndexOutOfRangeException:索引超出了数组的范围。 (wrapper managed-to-managed) System.Object.ElementAddr_4(object,int,int,int) Camera_s.Start () (at Assets/scripts/Camera_s.cs:19)

Script Move.cs:

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

public class Move : MonoBehaviour
{
    public Block scr;
    public Camera_s cam;

    public Transform my_block;
    private int[,,] grid;
    void Start()
    {
        my_block = gameObject.transform.parent;
        scr = my_block.GetComponent<Block>();
        cam = GameObject.Find("Main Camera").GetComponent<Camera_s>();
        grid = cam.Grid;
    }

    void OnMouseDown(){
        if (scr.IsActive){
            if (gameObject.transform.name == "left"){
                my_block.position -= new Vector3(cam.Jump_Size, 0f);
            }
            else if (gameObject.transform.name == "right"){
                my_block.position += new Vector3(cam.Jump_Size, 0f);
            }
            else if (gameObject.transform.name == "up"){
                my_block.position += new Vector3(0f, cam.Jump_Size);
            }
            else if (gameObject.transform.name == "down"){
                my_block.position -= new Vector3(0f, cam.Jump_Size);
            }
        }
    }

    void Update()
    {
        
    }
}
Script Block.cs:

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

public class Block : MonoBehaviour
{
    public bool IsActive = false;

    public Camera_s Camera_Script;

    public int Id;
    public int[] coords;
    public int color;
    private int[,,] grid;

    void Start()
    {
        Camera_Script = GameObject.Find("Main Camera").GetComponent<Camera_s>();
        Id = Camera_Script.Block_max + 1;
        Camera_Script.Block_max += 1;
        grid = Camera_Script.Grid;
        grid[coords[0], coords[1], 0] = 1;
        grid[coords[0], coords[1], 0] = color;
    }

    void OnMouseDown(){
        IsActive = true;
        Camera_Script.Active = Id;
        Debug.Log("Active");
    }

    // Update is called once per frame
    void Update()
    {
        if (Camera_Script.Active != Id){
            IsActive = false;
        }
    }
}
Script Camera_s.cs:

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

public class Camera_s : MonoBehaviour
{
    public int Active = -1;
    public int Block_max = -1;


    public float Jump_Size;
    public int[] Grid_Size;
    public int[,,] Grid;
    void Start()
    {
        Grid = new int[Grid_Size[1], Grid_Size[0], 2];
        for (int i = 0;i < 3;i++){
            for (int j = 0;j< 5;j++){
                Grid[j, i, 0] = 0;
                Grid[j, i, 1] = 0;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

我理解我的 for 有问题,但我不知道如何解决。

代码的主要部分,我有错误:

for (int i = 0;i < 3;i++){
            for (int j = 0;i < 5;j++){
                Grid[j, i, 0] = 0;
                Grid[j, i, 1] = 0;
            }
        }

Unity Editor 输入的数据:

Jump_Size = 3f; Grid_Size = [5, 3]

谢谢!

【问题讨论】:

    标签: c# arrays unity3d


    【解决方案1】:

    如果您输入的确实是 Grid_Size = [5, 3],这意味着您的前两个维度被翻转。 您可以简单地纠正它,但如果只有一个值对产生错误,则不应公开大小。

    Grid = new int[Grid_Size[0], Grid_Size[1], 2];
    for (int i = 0;i < Grid.GetLength(1);i++){
        for (int j = 0;j< Grid.GetLength(0);j++){
            Grid[j, i, 0] = 0;
            Grid[j, i, 1] = 0;
        }
    }
    

    如果翻转输入,将产生与以前相同的数组维度,但仍使用与以前相同的访问顺序。当然,具体如何执行此操作取决于您要对数组做什么。

    此外,建议检查输入的正确值,在 Block 中,您再次使用来自编辑器的输入(坐标)为 Grid 编制索引。我建议确保输入在数组的预期范围内

    【讨论】:

      猜你喜欢
      • 2011-04-15
      • 2018-10-26
      • 1970-01-01
      相关资源
      最近更新 更多