Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges