#gameobject in 2d array
1 messages · Page 1 of 1 (latest)
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
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
so MazeGeneration is my script that cells is created in
apologies for the comments making this chunkier than necessary
And what error do you get?
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
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?
thing is VS is okay with if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null)
just unity isnt
also why does MazeAlgorithm inherit from MazeGeneration
What does Unity say is the error then
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)
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..
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
Yeah, you have to put <MazeGeneration> there, as said, I only said that thing because you stated cells would be in Cell
at first I made it inherit because i thought that would let me access the variables without using GameObject.Find but i was wrong lol
how would i correct this?
uh
if (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north != null)
?
yeah how would i change this to specify that cells is in the class Cell
that just assigns the cells variable an array of Cell
the cells variable is still in the MazeGeneration class though
ahhh ok
why does unity think (GameObject.Find("Generator").GetComponent<MazeGeneration>().cells[r, c].north doesn't exist
fixed that < columns thing?
yep
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?
just once
i tried that but the code was dodgy at that point so ill retry ir
holy shit it worked thanks a bunch
yeah i had thought of that before but the other holes in the code fucked it up lol