Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Edit GameObject variables within a multidimensional array

Discussion in 'Scripting' started by ugotstoopt, Feb 8, 2021.

  1. ugotstoopt

    ugotstoopt

    Joined:
    Dec 6, 2020
    Posts:
    2
    Hello, I'm trying to set up a simple grid for a turn-based RPG battle system similar to the old school FF series.

    I have created a 3 x 2 grid using arrays, and each array element contains a prefab game object ("spawn position"). These game objects will be used to spawn enemy characters during the battles.

    In this game there are different implications for a unit being placed in the front row as opposed to the back row, so I created the grid by defining two arrays of GameObjects - one for the front row and one for the back row. I then combine those two arrays into a multidimensional array.

    Currently the spawn position class, which is defined in a separate script file than where I am creating the grid, has a single variable: a boolean 'isEmpty'.

    Upon starting the game, I run a for loop to assign the Spawn Position game object into each array element.

    I'm now trying to toggle the isEmpty boolean for different spawn position objects by referencing the multidimensional array index, but can't figure out how to do that.

    Can someone help point me in the right direction as to how I can edit Game Object variables stored within arrays?

    My code is below:

    (Specifically I am hoping to manipulate the variables of the spawn position objects by referencing the multidimensional 'grid' array).

    Spawn Position code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spawnPosition : MonoBehaviour
    6. {
    7.     public bool isEmpty; // Check to see if a character can spawn/move here
    8.  
    9.     public GameObject character; // Which character if any is currently on the spot
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.        
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.        
    21.     }
    22. }
    Grid code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5.  
    6. public class battleGrid : MonoBehaviour
    7. {
    8.     public GameObject spawnPosition;
    9.  
    10.     static int rowLength = 3;
    11.  
    12.     public GameObject[] frontRow = new GameObject[rowLength];
    13.     public GameObject[] backRow = new GameObject[rowLength];
    14.  
    15.     public GameObject[,][] grid = new GameObject[2,1][];
    16.  
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.  
    22.         for (int i = 0; i < frontRow.Length; i++)
    23.         {
    24.             frontRow[i] = spawnPosition;
    25.         }
    26.  
    27.         for (int i = 0; i < backRow.Length; i++)
    28.         {
    29.             backRow[i] = spawnPosition;
    30.         }
    31.  
    32.         grid[0, 0] = frontRow;
    33.         grid[1, 0] = backRow;
    34.  
    35.     }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.  
    41.     // Here is where I want to manipulate the values of different Spawn Position game objects within the grid array
    42.  
    43.     }
    44. }
    45.  

    Thank you
     
  2. ugotstoopt

    ugotstoopt

    Joined:
    Dec 6, 2020
    Posts:
    2
    I figured it out. I had to make the arrays of spawnPosition type, as opposed to GameObject. Whoops.