I am attempting to assign an integer value to a 2d array, but I get a null reference error✅

The specific error is "NullReferenceException: Object Reference Not Set To An Instance Of An Object". Here is my code:
No description
28 Replies
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
tileBuildings is a 2 dimensional int array currentlySelectedTile is a Vector2Int variable
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
all references have been assigned
No description
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I defined it's size here
No description
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
tileCountX and TileCountY I defined in the game editor; they are public variables
Buddy
Buddy6mo ago
In many places here you can get null reference exception Please code safely Don't just assume the object always exists
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
which object? I am confused
Buddy
Buddy6mo ago
Please use $paste
MODiX
MODiX6mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Buddy
Buddy6mo ago
Screenshots are by far worse unless it is important, like showing unity inspector, errors and so forth.
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
ok
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
BlazeBin - okhzdwzmtodc
A tool for sharing your source code with the world!
Buddy
Buddy6mo ago
And which line do you get the error? Is it in this code snippet?
manager.GetComponent<GameManager>().tileBuildings[playerCamera.GetComponent<clickOnTile>().currentlySelectedTile.x, playerCamera.GetComponent<clickOnTile>().currentlySelectedTile.y] = 1;
manager.GetComponent<GameManager>().tileBuildings[playerCamera.GetComponent<clickOnTile>().currentlySelectedTile.x, playerCamera.GetComponent<clickOnTile>().currentlySelectedTile.y] = 1;
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
yes one moment, my entire game seems to be broken, and I am not sure why
Buddy
Buddy6mo ago
Your code doesn't seem complete missing class and few variables
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I have not pasted in anything that is not related to the issue, let me recheck to ensure I have gotten everything important
Buddy
Buddy6mo ago
Would you mind pasting the entire class?
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I will
Buddy
Buddy6mo ago
Either way. Everytime you have a GetComponent you assume it exists, but what happens if it doesn't? Also you can save performance by saving the components into cache and re-use them GetComponent is fairly expensive
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I know that these components exist, I will look into using the cache
Buddy
Buddy6mo ago
I suggest using the debugger
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
now I am recieving the error somewhere else and it has completely halted my code, will change the pasted code to that
Buddy
Buddy6mo ago
Set a breakpoint at that line, attach the debugger run the game in debug mode. Whenever the game will execute said line it will break and you can see the values in your IDE Assuming you use Visual Studio Community 2022 or JetBrains Rider
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
I have updated the pasted code will do
Buddy
Buddy6mo ago
You must re-paste the link as the link changes when you make changes (and save)
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
ah
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
BlazeBin - tzqzlapbhima
A tool for sharing your source code with the world!
Buddy
Buddy6mo ago
Okay, so let me show you where the possibilities of a null reference exception in this snippet can be. - GetComponent<GameManager>() can return null - which can lead to a null reference exception - tileBuildings can be null - which can lead to a null reference exception - GetComponent<clickOnTile>() can be null - which can lead to a null reference exception - currentlySelectedTile can be null - which can lead to a null reference exception - manager can be null - which can lead to a null reference exception (But I see you have set it in your inspector) So if any of those fails, it will throw a null reference exception I suggest you split up your code and prefer as I mentioned earlier, save it into cache. Assuming the components never changes transform. You can save them into cache in Awake or Start
private clickOnTile myComponent;
void Start()
{
myComponent = GetComponent<clickOnTile>();
}
private clickOnTile myComponent;
void Start()
{
myComponent = GetComponent<clickOnTile>();
}
Also note that your manager is already of type GameManager, so using GetComponent<GameManager>() on a type of already GameManager doesn't make sense
41phaNum3r1ca1
41phaNum3r1ca1OP6mo ago
oh my god I get now why I couldnt call a variable from a reference earlier see, I made a reference to a gameobject and tried to go gameobject.variable and was confused that I had to gameobject.GetComponent<ScriptName>().variable when for the game manager I could just manager.variable it is because the game manager class is a script, not a type of game object! I have determinded the null value I had forgotten to set a reference in a different script, which was somehow causing a null value here this is solved ✅
Want results from more Discord servers?
Add your server