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
tileBuildings is a 2 dimensional int array currentlySelectedTile is a Vector2Int variable
41phaNum3r1ca1
all references have been assigned
No description
41phaNum3r1ca1
I defined it's size here
No description
41phaNum3r1ca1
tileCountX and TileCountY I defined in the game editor; they are public variables
Buddy
Buddy4w ago
In many places here you can get null reference exception Please code safely Don't just assume the object always exists
41phaNum3r1ca1
which object? I am confused
Buddy
Buddy4w ago
Please use $paste
MODiX
MODiX4w 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
Buddy4w ago
Screenshots are by far worse unless it is important, like showing unity inspector, errors and so forth.
41phaNum3r1ca1
ok
41phaNum3r1ca1
BlazeBin - okhzdwzmtodc
A tool for sharing your source code with the world!
Buddy
Buddy4w 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
yes one moment, my entire game seems to be broken, and I am not sure why
Buddy
Buddy4w ago
Your code doesn't seem complete missing class and few variables
41phaNum3r1ca1
I have not pasted in anything that is not related to the issue, let me recheck to ensure I have gotten everything important
Buddy
Buddy4w ago
Would you mind pasting the entire class?
41phaNum3r1ca1
I will
Buddy
Buddy4w 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
I know that these components exist, I will look into using the cache
Buddy
Buddy4w ago
I suggest using the debugger
41phaNum3r1ca1
now I am recieving the error somewhere else and it has completely halted my code, will change the pasted code to that
Buddy
Buddy4w 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
I have updated the pasted code will do
Buddy
Buddy4w ago
You must re-paste the link as the link changes when you make changes (and save)
41phaNum3r1ca1
ah
41phaNum3r1ca1
BlazeBin - tzqzlapbhima
A tool for sharing your source code with the world!
Buddy
Buddy4w 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
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
More Posts