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 lm2malicl · Jan 07, 2016 at 11:16 PM · instantiatenullreferenceexceptionarray of gameobjects

Instantiating GameObjects From an array gives Null Reference Exception please help

Hello,

I am trying to instantiate a grid of gameObjects with the type of game object being stored in an array which is populated at runtime. The code runs fine when on its own (the top code). When I remove extends monobehavior and function call and set as an object in a different script (the bottom code). I get:

"NullReferenceException: Object reference not set to an instance of an object MapMaker.GenerateMap () (at Assets/Script/BoardScripts/MapMaker.cs:136) MapMaker.SetBoard (Int32 size, Int32 numberPlayers) (at Assets/Script/BoardScripts/MapMaker.cs:127) MapTest.Start () (at Assets/Script/BoardScripts/MapTest.cs:8)"

I am by no means an expert on unity or c# and am I'm very confused. Any explanation as to why it stops working would be very helpful.

Code that works

 using UnityEngine;
 using System.Collections;
 
 public class MapMaker : MonoBehaviour {
     public GameObject[] visuals;
     public int[,] board;
     
     void Start(){
         visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
         visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
         SetBoard (13, 4);
     }
 
     public void SetBoard(int size, int numberPlayers){
         board = new int[size, size];
         switch (size) {
         case 11://3*3
             switch (numberPlayers) {
             case 2:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 break;
             case 3:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 board [10, 5] = 9;
                 break;
             case 4:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 board [10, 5] = 9;
                 board [0, 5] = 10;
                 break;
             }
             break;
         case 13://4*4
             switch (numberPlayers) {
             case 2:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 break;
             case 3:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 board [12, 6] = 9;
                 break;
             case 4:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 board [12, 6] = 9;
                 board [0, 6] = 10;
                 break;
             }
             break;
         case 15://5*5
             switch (numberPlayers) {
             case 2:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 break;
             case 3:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 board [14, 7] = 9;
                 break;
             case 4:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 board [14, 7] = 9;
                 board [0, 7] = 10;
                 break;
             }
             break;
         case 17://6*6
             switch (numberPlayers) {
             case 2:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 break;
             case 3:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 board [16, 8] = 9;
                 break;
             case 4:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 board [16, 8] = 9;
                 board [0, 8] = 10;
                 break;
             }
             break;
         case 19://7*7
             switch (numberPlayers) {
             case 2:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 break;
             case 3:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 board [18, 9] = 9;
                 break;
             case 4:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 board [18, 9] = 9;
                 board [0, 9] = 10;
                 break;
             }
             break;
         }
         GenerateMap();
         Debug.Log ("It Called the GenerateMap");
     }
 
 public void GenerateMap(){
     int cnt = 0;
     for (int i = 0; i<board.GetLength(0); i++) {
         for (int j = 0; j<board.GetLength(1); j++) {
                 int type = board[i,j];
                 GameObject.Instantiate(visuals[type],new Vector3(i,-j,0),Quaternion.identity);
             }
         }
     }
 }

Code that doesn't work

 using UnityEngine;
 using System.Collections;
 
 public class MapMaker {
     public GameObject[] visuals;
     public int[,] board;
     
     void Start(){
         visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
         visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
     }
 
     public void SetBoard(int size, int numberPlayers){
         board = new int[size, size];
         switch (size) {
         case 11://3*3
             switch (numberPlayers) {
             case 2:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 break;
             case 3:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 board [10, 5] = 9;
                 break;
             case 4:
                 board [5, 5] = 6;
                 board [5, 10] = 7;
                 board [5, 0] = 8;
                 board [10, 5] = 9;
                 board [0, 5] = 10;
                 break;
             }
             break;
         case 13://4*4
             switch (numberPlayers) {
             case 2:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 break;
             case 3:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 board [12, 6] = 9;
                 break;
             case 4:
                 board [6, 6] = 6;
                 board [6, 12] = 7;
                 board [6, 0] = 8;
                 board [12, 6] = 9;
                 board [0, 6] = 10;
                 break;
             }
             break;
         case 15://5*5
             switch (numberPlayers) {
             case 2:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 break;
             case 3:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 board [14, 7] = 9;
                 break;
             case 4:
                 board [7, 7] = 6;
                 board [7, 14] = 7;
                 board [7, 0] = 8;
                 board [14, 7] = 9;
                 board [0, 7] = 10;
                 break;
             }
             break;
         case 17://6*6
             switch (numberPlayers) {
             case 2:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 break;
             case 3:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 board [16, 8] = 9;
                 break;
             case 4:
                 board [8, 8] = 6;
                 board [8, 16] = 7;
                 board [8, 0] = 8;
                 board [16, 8] = 9;
                 board [0, 8] = 10;
                 break;
             }
             break;
         case 19://7*7
             switch (numberPlayers) {
             case 2:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 break;
             case 3:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 board [18, 9] = 9;
                 break;
             case 4:
                 board [9, 9] = 6;
                 board [9, 18] = 7;
                 board [9, 0] = 8;
                 board [18, 9] = 9;
                 board [0, 9] = 10;
                 break;
             }
             break;
         }
         GenerateMap();
         Debug.Log ("It Called the GenerateMap");
     }
 
 public void GenerateMap(){
     int cnt = 0;
     for (int i = 0; i<board.GetLength(0); i++) {
         for (int j = 0; j<board.GetLength(1); j++) {
                 int type = board[i,j];
                 GameObject.Instantiate(visuals[type],new Vector3(i,-j,0),Quaternion.identity);
             }
         }
     }
 }

Class used to call the code that doesnt work:

 using UnityEngine;
 using System.Collections;
 
 public class MapTest : MonoBehaviour {
 
     void Start(){
         MapMaker map = new MapMaker ();
         map.SetBoard (13, 4);
     }
 }
 

 



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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by corn · Jan 07, 2016 at 11:36 PM

MapMaker is not a MonoBehaviour, its Start method won't be called on its own. Since Start is not executed, visuals has not been initialized by the time you try to generate the map.

Replace the Start method by a constructor, and that should do the trick.

  public class MapMaker {
      public GameObject[] visuals;
      public int[,] board;
      
      public MapMaker() {
          visuals = new GameObject[Resources.LoadAll<GameObject>("TilePrefabs").Length];
          visuals = Resources.LoadAll<GameObject> ("TilePrefabs") as GameObject[];
      }
 
 ...
 }
Comment
Add comment · Show 1 · Share
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
avatar image lm2malicl · Jan 08, 2016 at 01:13 AM 0
Share

Worked beautifully thank you so much!!!

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

41 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 avatar image avatar image avatar image avatar image

Related Questions

Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object. 1 Answer

How to instantiate an array of prefabs into an array of empty gameobject positions 0 Answers

NullException Error while instantiating gameobjects 0 Answers

I am trying to access transform[] and count from SCRIPT A, however, its not working perfectly. SCRIPT B didnt apply the effect onto the instantiated fish from SCRIPT A. Only some fishes fly to the targets. 0 Answers

Unity Networking Client NullReferenceException Error 0 Answers


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