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