Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D Grid Placing cells into array

Discussion in 'Scripting' started by Deleted User, Jul 13, 2014.

  1. Deleted User

    Deleted User

    Guest

    (please ignore fact that they arent actually grids I used spheres to get quicker to scripting)
    I am able to generate hex grid like this and what I wanna to do is to do relative adresing every cell with each other or more likely to put them into array my problem lies in that when I tried to put them into array while creating them I failed many times so I am not sure how to do it
     

    Attached Files:

  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    It would help immensely to know in which ways you failed. "I failed" is not a problem description one can base a help on.

    Please use code tags when you post code. Hardly anyone will read this mess.

    My suggestion when dealing with hexagons is not to use an array but a dictionary instead (especially when you work with "circular" maps). the hex in the center has the hexcoordinate 0,0,0 and then you define your 3 axis and then each hex has a certain hexcoordinate (fe +001-002+000, note the leading zeros and the mandatory sign). this coordinate string can then be used as key in a dictionary.

    And depending on the number of total hexes you aim for it may be inappropriate to use a gameobject for each.
     
  3. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Hex : MonoBehaviour {
    5.     public Transform spawnThis;  
    6.    
    7.     public int x = 5;
    8.     public int y = 5;
    9.  
    10.  
    11.  
    12.     GameObject[,] cells= new GameObject[5,5];
    13.     GameObject[,] grid= new GameObject[5,5];
    14.     bool[,] free= new bool[5,5];
    15.     int[,] direction= new int[5,5];
    16.     int[,,] properties= new int[5,5,6];
    17.  
    18.  
    19.     void Start() {
    20.         int i, j;
    21.         free[4,3]=true;
    22.         for( i = 0; i < x; i++ ) {
    23.             for( j = 0; j < y; j++ ) {
    24.                 if(2<(i+1)*(j+1) && (i+1)*(j+1)<20){
    25.                 Vector2 hexpos = HexOffset( i+1, j+1 );
    26.                 Vector3 pos = new Vector3( hexpos.x, hexpos.y, 0 );
    27.                 cells[i,j] = Instantiate(spawnThis, pos, Quaternion.identity ) as GameObject;
    28.                 }
    29.             }
    30.         }
    31.  
    32.  
    33.  
    34.     }
    35.    
    36.     Vector2 HexOffset( int x, int y ) {
    37.         Vector2 position = Vector2.zero;
    38.        
    39.  
    40.             position.x = (x *1.75f - 5.25f)  ;
    41.             position.y = (y * 2 - 9 + x) ;
    42.    
    43.  
    44.        
    45.         return position;
    46.     }
    47.  
    48.     void Update()
    49.     {
    50.         int i, j;
    51.        
    52.         for( i = 0; i < x; i++ ) {
    53.             for( j = 0; j < y; j++ ) {
    54.                 if(2<(i+1)*(j+1) && (i+1)*(j+1)<20){
    55.                     if(free[i,j])
    56.                     {
    57.                         cells[i,j].renderer.material.color= UnityEngine.Color.green;
    58.                     }
    59.                     else
    60.                     {
    61.                         cells[i,j].renderer.material.color= UnityEngine.Color.red;
    62.                     }
    63.                 }
    64.             }
    65.         }
    66.     }
    67.  
    68. }
    It gives me this error :NullReferenceException: Object reference not set to an instance of an object Hex.Update () (at Assets/Hex.cs:61)
    So I am asking how to get object reference.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    when you double click the errormessage you should be taken to the offending line. then check all reference types for null (fe via debug.log or an if). so you can identify what is null and then fill it properly. from your line i would guess that either cells[i,j] is null because it was not filled or that you have no renderer attached.
     
  5. Deleted User

    Deleted User

    Guest

    Cells should be attached during Start()
    The Object I copy have renderrer and so does all the copies.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Well, I can only guess. The only one who can actually find out which value is null is YOU. I have told you some ways to achieve this so why don't you try them?
    You say cells SHOULD be attached during Start. Instantiate can fail, start may not be executed when the object is disabled by default, components can be removed etc so there are many things which can go wrong. And they will frequently do and its better you have some methodology to resolve such issues in your belt.

    As another remark: There are at least 5 hex managing packages in the asset store and forceX offers one for free in the forum.