Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

NullReferenceException: Object reference not set to an instance of an object

$
0
0
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; } }

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>