public int[,] ballCoord;
private void Start()
{
GameGenerator(5, 10); // just for trying. I have another code to create x and xGG.
}
public void GameGenerator(int x, int xGG)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < 2; j++)
{
int k = Random.Range(0, xGG);
Debug.Log(k);
ballCoord[i, j] = k; // getting a null exception
}
}
}
I also tried this:
for (int i = 0; i < x; i++)
{
int j = Random.Range(0, xGG);
int k = Random.Range(0, xGG);
ballCoord = new int[,] { { j, k } }; // all created numbers are assigned in [0, 0]
}
What is my purpose?
Instead of creating and storing new scenes, I’m thinking of saving games using a text file. I need that random coordinates. I don’t want to create one by one. Because i have more than 600 games.
How do i fix that null exception?
Or is my approach wrong.
It simply does not matter what you are doing, doesn’t matter even in the least.
The answer is always the same for null reference… ALWAYS. It is the single most common error ever. Don’t waste your life on this problem. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
I already did but after a lot of attempts and errors, I added that section to the comment line for my last great idea. And I cleared all comment sections before posting.
public void GameGenerator(int x, int xGG)
{
ballCoord = new int[2, x];
for ...
}
Idea I used is the same as in the document:
I’ll try something else after a little break.
Thanks to both of you for your help.