Jump to content

Unity Issue (C#)

Go to solution Solved by charles245,

I'll go test it myself to be sure, but it looks like you forgot to initalize your 2D array upon calling the GenerateMap function.

I'm trying to get a simple map generation script to work. Here's my code:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateLevel : MonoBehaviour {

	public GameObject square;
	public GameObject[,] map; 

	void Start () 
	{
		GenerateMap (20, 10);
	}

	public void GenerateMap (int x, int y) 
	{
		for (int a = 0; a < x; a++) 
		{
			for(int b = 0; b < y; y++) 
			{
				Vector3 vect = new Vector3 (a, b, 0);
				GameObject tile = (GameObject)Instantiate(square, vect, Quaternion.identity, this.transform);
				map [a, b] = tile;
			}
		}
	}
}

Basically, I have this script attached to an object in the scene and have a valid prefab reference to the "square." I have no other code or scripts but this. When I run the scene, the program begins to gobble RAM until it crashes. A couple of times, it spawn one square and said that there was an null object reference to tile at "map [a, b] = tile;"

 

Please help.

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
https://linustechtips.com/topic/859800-unity-issue-c/
Share on other sites

Link to post
Share on other sites

Just now, charles245 said:

I'll go test it myself to be sure, but it looks like you forgot to initalize your 2D array upon calling the GenerateMap function.

I just found the problem, I did:

for(int b = 0; b < y; y++)

and not:

for(int b = 0; b < y; b++)

However, I still am having a problem with this:

"null value found where object instance required at line 23"

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
https://linustechtips.com/topic/859800-unity-issue-c/#findComment-10698216
Share on other sites

Link to post
Share on other sites

2 minutes ago, charles245 said:

I'll go test it myself to be sure, but it looks like you forgot to initalize your 2D array upon calling the GenerateMap function.

Oh ok, now I get what you are saying. I'll go test that.

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
https://linustechtips.com/topic/859800-unity-issue-c/#findComment-10698218
Share on other sites

Link to post
Share on other sites

4 minutes ago, TurbulentWinds said:

I just found the problem, I did:


for(int b = 0; b < y; y++)

and not:


for(int b = 0; b < y; b++)

However, I still am having a problem with this:

"null value found where object instance required at line 23"

Oh, of course! Can't believe I ran right past that myself. Anyways, to fix the other error just initalize the array in the function.


map = new GameObject[x, y];

Link to comment
https://linustechtips.com/topic/859800-unity-issue-c/#findComment-10698221
Share on other sites

Link to post
Share on other sites

3 minutes ago, charles245 said:

I'll go test it myself to be sure, but it looks like you forgot to initalize your 2D array upon calling the GenerateMap function.

Yep. That was issue number 2. Thanks!

ORANGE SCREEN WINDOWS 10 VALUE OVER TIME - PC VS MAC

Spoiler

i5 7600k @ 5.0 GHz xD

Corsair H60 with Noctua NF-F12 iPPC-3000 PWM

MSI Z270-A Pro Motherboard

EVGA 1050 Ti SC

16 GB Corsair DDR4 @ 2400 MHz

500 GB Sandisk 950 PRO - Windows 10, Elementary OS, Zorin OS

500 GB Sandisk 850 PRO

1 TB WD Blue

Corsair CX750

1 x Corsair AF120 Quiet Red Led

Rosewell Tyrfing Case

Spoiler

EliteBook 8570w
i7 3720QM @ 2.6 GHz
Quadro K1000M
24 GB DDR3 @ 1600 MHz
250 GB SanDisk 850 EVO - Elementary OS, Windows 10, Debian

Spoiler

i5 3470 @ 3.2 GHz
EVGA 750 Ti SC
8 GB DDR3 @ 1333 MHz
240 GB SanDisk - Windows 10, Linux Mint

 

Link to comment
https://linustechtips.com/topic/859800-unity-issue-c/#findComment-10698222
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×