- Home /
Object reference not set to an instance of an object error C#
Just an error that I don't posses the skill to fix. The error is on line 63.
using UnityEngine;
using System.Collections;
public class floorbuilder : MonoBehaviour {
//floors
public GameObject[] fourrooms;
public GameObject[] threerooms;
public GameObject[] tworooms;
public GameObject[] onerooms;
public GameObject[] startrooms;
GameObject[] currentfloor4;
GameObject[] currentfloor3;
GameObject[] currentfloor2;
GameObject[] currentfloor1;
//data containment
public static int floornumb = 1;
float doornumb;
float roomnumb = 4;
float fourdoornumb;
float threedoornumb;
float twodoornumb;
float onedoornumb;
public int Size_Increase_Factor = 3;
//positional variables
int xnumb = 0;
int znumb = 0;
float snumb = 0;
float rnumb = 0;
void Start () {
transform.position = new Vector3 (0, 0, 0);
buildnextfloor ();
}
void buildnextfloor(){
//Math determining the number of rooms and doors relitive to the floor level and shape of room positioning
roomnumb = /*(insert mathy stuff stuff here)*/ (roomnumb + roomnumb) * Size_Increase_Factor;
fourdoornumb = (Mathf.Round(roomnumb * (Random.Range(0.2f, 0.5f))));
threedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.4f))));
twodoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
onedoornumb = (Mathf.Round(roomnumb * (Random.Range(0.1f, 0.3f))));
doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
if (doornumb % 2 == 1) {
onedoornumb ++;
doornumb = (fourdoornumb * 4) + (threedoornumb * 3) + (twodoornumb * 2) + (onedoornumb);
roomnumb = (fourdoornumb) + (threedoornumb) + (twodoornumb) + (onedoornumb);
}
Debug.Log ("Number of rooms: " + roomnumb);
Debug.Log ("Number of doors: " + doornumb);
Debug.Log ("4doors: " + fourdoornumb);
Debug.Log ("3doors: " + threedoornumb);
Debug.Log ("2doors: " + twodoornumb);
Debug.Log ("1door: " + onedoornumb);
//Selection of rooms
for (int i = 0; i < fourdoornumb; i++) {
currentfloor4[i] = fourrooms[(Random.Range(0, fourrooms.Length))];
}
for (int i = 0; i < threedoornumb; i++) {
currentfloor3[i] = threerooms[(Random.Range(0, threerooms.Length))];
}
for (int i = 0; i < twodoornumb; i++) {
currentfloor2[i] = tworooms[(Random.Range(0, tworooms.Length))];
}
for (int i = 0; i < onedoornumb; i++) {
currentfloor1[i] = onerooms[(Random.Range(0, onerooms.Length))];
}
snumb = (Mathf.Round((Mathf.Sqrt (roomnumb))));
rnumb = roomnumb - (snumb * snumb);
snumb = snumb * 100;
rnumb = rnumb * 100;
//placing of rooms
Instantiate (startrooms[Random.Range(0, startrooms.Length)], transform.position, transform.rotation);
updatelocation ();
for(int i = 0; i < fourdoornumb; i ++){
Instantiate(currentfloor4[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < threedoornumb; i ++){
Instantiate(currentfloor3[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < twodoornumb; i ++){
Instantiate(currentfloor2[i], transform.position, transform.rotation);
updatelocation();
}
for(int i = 0; i < onedoornumb; i ++){
Instantiate(currentfloor1[i], transform.position, transform.rotation);
updatelocation();
}
}
void updatelocation(){
if(xnumb < snumb){
xnumb += 100;
}
else if (znumb < snumb){
znumb += 100;
xnumb = 0;
}
else{
znumb += 100;
}
transform.position = new Vector3 (xnumb, 0f, znumb);
}
}
Any help would be awesome.
Answer by Simon-Larsen · Mar 26, 2014 at 12:44 AM
After looking at your code again I can see the error lies in your fields. You have not yet initialized your GameObject arrays
GameObject[] currentfloor4;
When must initiatlize your array, you must know how many elements you're gonna use, example:
GameObject[] gameObjects = new GameObject[10] // array lenght of 10
You can also initialize the arrays in the Start() function and in that function determine the length you're going to need on that array.
Now, while I have not thoroughly read through your code, my suggestion is that what you're looking for is actually a dictionary. I've found this page to hold some nice information about just that http://robotduck.wordpress.com/2009/11/04/88/
I initialized my room arrays, and for the arrays that need to vary in size, I used array lists. $$anonymous$$y scripts kinda sorta works now! thanks!
Your answer
Follow this Question
Related Questions
Random NullReferenceException 0 Answers
Distribute terrain in zones 3 Answers
NullReferenceException error in an array of objects 0 Answers
Error CS0029 Help? (Screenshot of Exact Error) 1 Answer
Object reference not set to an instance of an object 1 Answer