Question by
Velvacaine · Oct 18, 2015 at 04:30 AM ·
errorgameobjectarrayobjectclass
NullReferenceException: Object reference not set to an instance of an object
So I've got a public class, and I'm trying to create an array of this class and pass it to a function to initialize it, but I get this error on the first line, during runtime.
NullReferenceException: Object reference not set to an instance of an object BoardDetector.initialize_board_state (.boardstate[] BoardState) (at Assets/Scripts/BoardDetector.cs:43) BoardDetector.Start () (at Assets/Scripts/BoardDetector.cs:27)
using UnityEngine;
using System.Collections;
using System;
[System.Serializable]
public class boardstate
{
public string name;
public bool active;
public float x;
public float z;
}
public class BoardDetector : MonoBehaviour {
private RaycastHit hit;
private Ray ray;
public boardstate[] BoardState;
public float[] point;
// Use this for initialization
void Start () {
BoardState = new boardstate[32];
initialize_board_state (BoardState);
}
// Update is called once per frame
void Update () {
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit) && Input.GetMouseButtonUp(0)) {
point [0] = hit.point.x >= 0 ? (float)((int)hit.point.x) + 0.5f : (float)((int)hit.point.x) - 0.5f;
point [1] = hit.point.z >= 0 ? (float)((int)hit.point.z) + 0.5f : (float)((int)hit.point.z) - 0.5f;
}
}
void initialize_board_state(boardstate[] BoardState)
{
for (int i = 0; i < 8; i++) {
BoardState[i].name = "wp";
BoardState[i].active = true;
BoardState[i].x = -3.5f + (float)i;
BoardState[i].z = -2.5f;
}
for (int i = 8; i < 10; i++) {
BoardState [i].name = "wr";
BoardState [i].active = true;
BoardState [i].x = -3.5f + (float)(7 * (i - 8));
BoardState [i].z = -3.5f;
}
for (int i = 10; i < 12; i++) {
BoardState [i].name = "wk";
BoardState [i].active = true;
BoardState [i].x = -2.5f + (float)(5 * (i - 10));
BoardState [i].z = -3.5f;
}
for (int i = 12; i < 14; i++) {
BoardState [i].name = "wb";
BoardState [i].active = true;
BoardState [i].x = -1.5f + (float)(3 * (i - 12));
BoardState [i].z = -3.5f;
}
BoardState [14].name = "wq";
BoardState [14].active = true;
BoardState [14].x = -0.5f;
BoardState [14].z = -3.5f;
BoardState [15].name = "wg";
BoardState [15].active = true;
BoardState [15].x = 0.5f;
BoardState [15].z = -3.5f;
for (int i = 16; i < 24; i++) {
BoardState [i].name = "bp";
BoardState [i].active = true;
BoardState [i].x = -3.5f + (float)i - 16.0f;
BoardState [i].z = 2.5f;
}
for (int i = 24; i < 26; i++) {
BoardState [i].name = "br";
BoardState [i].active = true;
BoardState [i].x = -3.5f + (float)(7 * (i - 24));
BoardState [i].z = 3.5f;
}
for (int i = 26; i < 28; i++) {
BoardState [i].name = "bk";
BoardState [i].active = true;
BoardState [i].x = -2.5f + (float)(5 * (i - 10));
BoardState [i].z = 3.5f;
}
for (int i = 28; i < 30; i++) {
BoardState [i].name = "bb";
BoardState [i].active = true;
BoardState [i].x = -1.5f + (float)(3 * (i - 12));
BoardState [i].z = 3.5f;
}
BoardState [30].name = "bq";
BoardState [30].active = true;
BoardState [30].x = 0.5f;
BoardState [30].z = 3.5f;
BoardState [31].name = "bg";
BoardState [31].active = true;
BoardState [31].x = -0.5f;
BoardState [31].z = 3.5f;
}
}
Comment
Your answer
Follow this Question
Related Questions
One script with multiple Gameobject only one Works 0 Answers
Having problems with custom objects in my array - BCE0019 0 Answers
ERROR CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `TriggerSphere' 1 Answer
Cant define array from other script 1 Answer
Array index out of range(C#) 1 Answer