Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by isse26 · Sep 06, 2014 at 11:58 PM · arraynullreferenceexceptiongridaccessing

Object reference not set when referencing it and adding to array

Hi. I am attempting to generate a grid and storing the individual Tiles in a 2 dimensional array. But as soon as I am attempting to access the the tiles in the array I get a "null reference exception" Bellow I have included some of the code, if someone could tell me what I'm doing wrong.

Strip down grid generation script:

 public class Grid : MonoBehaviour
 {
     public Tile[,] Map;
  
     public int gridWidthInHexes = 10;
     public int gridHeightInHexes = 10;
  
     private void createGrid()
     {
         int tempTileIndex = 0;
         int curMapIndexHeight = 0;
         for (float y = 0; y < gridHeightInHexes; y++)
         {
             int curMapIndexWidth = 0;
             for (float x = 0; x < gridWidthInHexes; x++)
             {
                 var hex = (GameObject) Instantiate(Hex);
                 var gridPos = new Vector2(x, y);
                 var tempTile = hex.GetComponent<Tile>();
                 tempTile.TileIndex = tempTileIndex;
                 Map[curMapIndexWidth, curMapIndexHeight] = hex.GetComponent<Tile>();
                 tempTileIndex++;
                 curMapIndexWidth++;
             }
             curMapIndexHeight++;
         }
     }
     private void Start()
     {
         Map = new Tile[gridWidthInHexes,gridHeightInHexes];
         createGrid();
     }
 }

Strip down game manager script:

 public partial class GameMaster : MonoBehaviour
 {
     private Grid grid;
     public GameObject unit;
     void Start()
     {
         grid = gameObject.GetComponent<Grid>();
     }
     void OnMapLoaded()
     {
         Debug.Log(grid.Map[1, 3].TileIndex);
     }
 }

The Tile script is not that interesting. It only contains some variables. Please help.

Comment
Add comment · Show 2
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 digibawb · Sep 07, 2014 at 12:24 AM 0
Share

Where is On$$anonymous$$apLoaded called from?

avatar image isse26 · Sep 07, 2014 at 10:48 AM 0
Share

The other part of the Game$$anonymous$$aster class

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by isse26 · Sep 07, 2014 at 01:10 PM

The Error massage is: NullReferenceException: Object reference not set to an instance of an object GameMaster.OnMapLoaded () (at Assets/_Scripts/GameMaster.cs:19) GameMaster._LoadedMap () (at Assets/_Scripts/GameEvents.cs:15) Grid.Start () (at Assets/_Scripts/Grid.cs:99)

Here are the entire scripts: Grid (should be attached to _GameMaster):

 using System.Collections.Generic;
 using UnityEngine;
 
 public class Grid : MonoBehaviour
 {
     private GameMaster gameMaster;
 
     //The Map
     
     public Tile[,] Map;
 
     //following public variable is used to store the hex model prefab;
     
     public GameObject Hex;
     //next two variables can also be instantiated using unity editor
     public int gridWidthInHexes = 10;
     public int gridHeightInHexes = 10;
 
     //Hexagon tile width and height in game world
     private float hexWidth;
     private float hexHeight;
 
     //Method to initialise Hexagon width and height
     private void setSizes()
     {
         //renderer component attached to the Hex prefab is used to get the current width and height
         hexWidth = Hex.renderer.bounds.size.x;
         hexHeight = Hex.renderer.bounds.size.z;
     }
 
     //Method to calculate the position of the first hexagon tile
     //The center of the hex grid is (0,0,0)
     private Vector3 calcInitPos()
     {
         Vector3 initPos;
         //the initial position will be in the left upper corner
         initPos = new Vector3(-hexWidth*gridWidthInHexes/2f + hexWidth/2, 0,
             gridHeightInHexes/2f*hexHeight - hexHeight/2);
 
         return initPos;
     }
 
     //method used to convert hex grid coordinates to game world coordinates
     public Vector3 calcWorldCoord(Vector2 gridPos)
     {
         //Position of the first hex tile
         Vector3 initPos = calcInitPos();
         //Every second row is offset by half of the tile width
         float offset = 0;
         if (gridPos.y%2 != 0)
             offset = hexWidth/2;
 
         float x = initPos.x + offset + gridPos.x*hexWidth;
         //Every new line is offset in z direction by 3/4 of the hexagon height
         float z = initPos.z - gridPos.y*hexHeight*0.75f;
         return new Vector3(x, 0, z);
     }
 
     //Finally the method which initialises and positions all the tiles
     private void createGrid()
     {
         Map = new Tile[gridWidthInHexes, gridHeightInHexes];
 
         //Game object which is the parent of all the hex tiles
         var hexGridGO = new GameObject("HexGrid");
         int tempTileIndex = 0;
         int curMapIndexHeight = 0;
         for (float y = 0; y < gridHeightInHexes; y++)
         {
             int curMapIndexWidth = 0;
             for (float x = 0; x < gridWidthInHexes; x++)
             {
                 //GameObject assigned to Hex public variable is cloned
                 var hex = (GameObject) Instantiate(Hex);
                 //Current position in grid
                 var gridPos = new Vector2(x, y);
                 var tempTile = hex.GetComponent<Tile>();
                 tempTile.TilePos = gridPos;
                 tempTile.TileIndex = tempTileIndex;
                 hex.transform.position = calcWorldCoord(gridPos);
                 hex.transform.parent = hexGridGO.transform;
                 Map[curMapIndexWidth, curMapIndexHeight] = hex.GetComponent<Tile>();
                 tempTileIndex++;
                 curMapIndexWidth++;
             }
             curMapIndexHeight++;
         }
     }
 
     //The grid should be generated on game start
     private void Start()
     {
         
         gameMaster = gameObject.GetComponent<GameMaster>();
         setSizes();
         createGrid();
         if (gameMaster != null)
         {
             gameMaster._LoadedMap();
         }
     }
 }
 

One of the GameMaster scripts should be attached to _GameMaster :

 using UnityEngine;
 
 public partial class GameMaster : MonoBehaviour
 {
     private Grid grid;
     public GameObject unit;
     void Awake()
     {
         MapLoaded += OnMapLoaded;
     }
     void Start()
     {
         grid = gameObject.GetComponent<Grid>();
     }
 
     void OnMapLoaded()
     {
         Debug.Log("Generated Map");
         Debug.Log(grid.Map[1, 3].TileIndex);
 
         
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             if (MouseClick != null) 
                 MouseClick(0);
         }
 
         if (Input.GetMouseButtonDown(1))
         {
             if (MouseClick != null)
                 MouseClick(1);
         }
 
         if (Input.GetMouseButtonDown(2))
         {
             if (MouseClick != null)
                 MouseClick(2);
         }
     }
 }
 
 public partial class GameMaster
 {
     public delegate void LoadingDeligate();
     public event LoadingDeligate MapLoaded;
 
     public delegate void ClickEvents(int button);
     public event ClickEvents MouseClick;
 
     public void _LoadedMap()
     {
         if (MapLoaded != null) 
             MapLoaded();
     }
 }
 

The tile script is attached to the tile Prefab:

 using UnityEngine;
 
 public class Tile : MonoBehaviour
 {
     private int _tileIndex;
     private Vector2 _tilePos;
     private GameObject _unit;
     private bool isHoverd;
     private GameObject GMObject;
     private GameMaster gameMaster;
     private bool isSelected;
 
     public Texture defultTexture;
     public Texture hoverTexture;
     public Texture selectedTexture;
 
     public int TileIndex
     {
         get { return _tileIndex; }
         set { _tileIndex = value; }
     }
     public Vector2 TilePos
     {
         get { return _tilePos; }
         set
         {
             _tilePos = value; 
             Debug.Log("New TilePos: " + _tilePos);
         }
     }
     public GameObject Unit
     {
         get { return _unit; }
         set { _unit = value; }
     }
 
     void Start ()
     {
         GMObject = GameObject.FindGameObjectsWithTag("GameController")[0];
         gameMaster = GMObject.GetComponent<GameMaster>();
         gameMaster.MouseClick += OnButtonPress;
     }
 
     void OnButtonPress(int button)
     {
         if (isHoverd && button == 0)
         {
             renderer.material.SetTexture("_MainTex", selectedTexture);
             isSelected = true;
         }
         else if (!isHoverd && button == 0 && !Input.GetKey(KeyCode.LeftShift))
         {
             renderer.material.SetTexture("_MainTex", defultTexture);
             isSelected = false;
         }
     }
 
     void OnMouseEnter()
     {
         if (!isSelected)
         {
             renderer.material.SetTexture("_MainTex", hoverTexture);            
         }
         isHoverd = true;
         Debug.Log("hovering: " + _tileIndex);
     }
 
     void OnMouseExit()
     {
         if (!isSelected)
         {
             renderer.material.SetTexture("_MainTex", defultTexture);
         }
         isHoverd = false;
     }
 }
 

Everything works with the exception of accessing the map array and the only error I get is when I'm trying to access the array with the map stored in.

Comment
Add comment · 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
0

Answer by digibawb · Sep 07, 2014 at 01:40 PM

Move your line:

 grid = gameObject.GetComponent<Grid>();

into the Awake method.

OnMapLoaded is most likely being called before the Start method is called, so grid is still null.

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 isse26 · Sep 07, 2014 at 02:55 PM 0
Share

Thanks that fixed it

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

RTS Grid Initialisation 0 Answers

NullReferenceException weird error 1 Answer

[c#] Create visible appearence of a grid using texture or models 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

Can't access Array in a class instance multidimensional Array. 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