-1

IndexOutOfRangeException: Index was outside the bounds of the array. (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()
    {
        
    }
}

I understand something wrong with my for, but i dont know how to fix it.

Main part of code, in which I had mistake:

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

Data inputed by Unity Editor:

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

Thanks!

farkon00
  • 109
  • 1
  • 8

1 Answers1

0

If what you input is really Grid_Size = [5, 3], that means your first two dimensions are flipped. You could simply correct that, but you should not expose the sizes if anything but one value pair is generating errors.

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;
    }
}

Would result in the same array dimensions as before if you flip the input, but still use the same access order as before. How exactly you do this depends on what you want to do with the array of course.

Additionally it would be advisable to check the input for correct values, in Block you are indexing Grid with input (coords) from the editor again. I'd recommend to make sure that input is in expected bounds of the array

Seneral
  • 327
  • 4
  • 12