Question by
Bruno-Silvaa · May 26, 2020 at 09:38 AM ·
array3d
How to store 3D objects position in an array.
I am trying to follow a 2D Pacman tutorial which i need to store all objects positions into an array. The problem is that i'm doing it 3D and the code is for 2D. How can I store vector3 position of all objects in an array and how can i find my Gameboard size since i am getting "index was outside the bounds of the array" error with everything i've tried?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameBoard : MonoBehaviour
{
private static int boardWidth = 28;
private static int boardHeight = 36;
public GameObject[,] board = new GameObject[boardWidth, boardHeight];
void Start()
{
Object[] objects = GameObject.FindObjectsOfType(typeof(GameObject));
foreach (GameObject o in objects)
{
Vector2 pos = o.transform.position;
if (o.name != "Player")
{
board [(int)pos.x, (int)pos.y] = o;
}
else
{
Debug.Log("Found Player at: " + pos);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Cant figure out how to implement a inventory system 1 Answer
Checking if index of Vector3 array matches integer with if statement? 0 Answers
Removing runtime array exception (C#) 1 Answer
Quaternion Roation Mouse Script 0 Answers
Cinemachine (or the unity compiler) broken due to some unknown reason 1 Answer