#gameobject in 2d array

1 messages · Page 1 of 1 (latest)

modest crag
#

I have a 2d array which has a gameObject assigned to it but can't seem to access it in another script

#

in one script I have this
public class Cell
{
public bool visited = false;
public GameObject north;
public GameObject east;
public GameObject south;
public GameObject west;
}

cells[rowCompare, colCompare].north = Instantiate(walls[0], new Vector3(rowCompare - 0.5f, 0, colCompare + 0.5f), Quaternion.identity);
"cells" is in the cell class which is how i've assigned a gameobject to the north wall

#

in another script i have this:

#

if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null)

#

which is trying to find game objects that have a north wall with given "coordinates" but unity seems to think this doesnt exist

thorn dome
#

Well, if cells is in the Cell class, it of course wouldn't exist in the MazeGeneration class that you're getting from GetComponent?

#

unless that was a typo :P

#

You'd probably want to (properly) paste your MazeGeneration class too

modest crag
#

so MazeGeneration is my script that cells is created in

#

apologies for the comments making this chunkier than necessary

thorn dome
#

And what error do you get?

modest crag
#

i replaced if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null) with ```if (GameObject.Find("Generator").GetComponent<Cell>().cells[r, c].north != null)

#
using UnityEngine;

public class MazeAlgorithm : MazeGeneration
{
    private int algRows;
    private int algColumns;
    private Cell[,] algCells;

    // Start is called before the first frame update
    void Start()
    {
        algRows = GameObject.Find("Generator").GetComponent<MazeGeneration>().rows;
        algColumns = GameObject.Find("Generator").GetComponent<MazeGeneration>().columns;
        Invoke("CopyCell", 2);
        StartAlgorithm();
    }

    private void CopyCell()
    {
        algCells = GameObject.Find("Generator").GetComponent<MazeGeneration>().cells;
    }

    private void StartAlgorithm()
    {
        for (int rowCompare = 0; rowCompare < algRows; rowCompare++)
        {
            for (int colCompare = 0; colCompare < algColumns; colCompare++)
            {
                AdjacencyCheck(rowCompare, colCompare);
            }
        }
    }

    private int AdjacencyCheck(int r, int c)
    {
        if (GameObject.Find("Generator").GetComponent<Cell>().cells[r, c].north != null)
        {
            Debug.Log(r);
            Debug.Log(c);
        }
        return (r);
    }

}
#

here's the second script btw im just trying to access the gameobject

thorn dome
#

WEll yeah I just said that because you say the cells thing would be in Cell or so, but it isn't anyway

#

What error does VS say exactly?

modest crag
#

thing is VS is okay with if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null)

#

just unity isnt

thorn dome
#

also why does MazeAlgorithm inherit from MazeGeneration

#

What does Unity say is the error then

modest crag
#

NullReferenceException: Object reference not set to an instance of an object
MazeAlgorithm.AdjacencyCheck (System.Int32 r, System.Int32 c) (at Assets/Scripts/MazeAlgorithm.cs:36)
MazeAlgorithm.StartAlgorithm () (at Assets/Scripts/MazeAlgorithm.cs:29)
MazeAlgorithm.Start () (at Assets/Scripts/MazeAlgorithm.cs:15)

thorn dome
#

for (int colCompare = 0; colCompare < rows; colCompare++) migth be your issue

#

should be < columns surely?

#

though you're breaking on that nayway

#

but if rows is lower than cols..

modest crag
#

oh shit that was stupid of me

#

never would have spotted that lol

#

if (GameObject.Find("Generator").GetComponent<Cell>().cells[r, c].north != null)
has the VS error

Error    CS1061    'MazeGeneration.Cell' does not contain a definition for 'cells' and no accessible extension method 'cells' accepting a first argument of type 'MazeGeneration.Cell' could be found (are you missing a using directive or an assembly reference?)    Assembly-CSharp, Assembly-CSharp.Player    C:\Users\Tahseen Khan\Desktop\maze generation\Assets\Scripts\MazeAlgorithm.cs    36    Active
thorn dome
#

Yeah, you have to put <MazeGeneration> there, as said, I only said that thing because you stated cells would be in Cell

modest crag
thorn dome
#

uh

#

if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null)

#

?

modest crag
#

getcomponent<mazegen>.getcomponent<cell> ?

#

oh

modest crag
thorn dome
#

?

#

It's not in the cells class

modest crag
#

cells = new Cell[rows, columns];

#

tho

thorn dome
#

that just assigns the cells variable an array of Cell

#

the cells variable is still in the MazeGeneration class though

modest crag
#

ahhh ok

#

why does unity think (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north doesn't exist

thorn dome
#

fixed that < columns thing?

modest crag
#

yep

thorn dome
#

The null error means that unity looks in the array at that location and finds a null, aka "no object". It's not that it doesn't find the variable.
But it finds null and then tries to access .north on that, so it errors.

Do you have MazeGeneration multiple times in your scene?

modest crag
#

just once

thorn dome
#

oh

#

Try invoking the StartAlgorithm too

#

so it runs a bit later

modest crag
#

i tried that but the code was dodgy at that point so ill retry ir

#

holy shit it worked thanks a bunch

thorn dome
#

xD

#

Yeah, the scripts don't run in a set order, so the array wasn't setup yet

modest crag
#

yeah i had thought of that before but the other holes in the code fucked it up lol