-1

I've got a token over a map in my (TEG/RISK wannabe) 2D game, I've divided the map in 16 different zones with a collider for each one so I can take the clicks over them and move the token towards it. I was able to do the basic move: token position = zone position, and it worked perfectly. The problem comes when I try to the algorithm restricting the movement to the token's neighboring areas, I get a lot of errors and when I get none it doesn't work.

The script is 200~ lines long (yeah, pretty sure is -100% optimized) so I'm gonna shorten some parts. It's supposed to be attached to each one of the 16 zones.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;


public class Movimiento : MonoBehaviour {

    //Prefabs of each zone
    public Transform OBJ_ZONA_1;
    public Transform OBJ_ZONA_2;
    .
    .
    .
    public Transform OBJ_ZONA_16;

    //List of zones prefabs to optimize searchs
    public List<Transform> LISTA_OBJ_ZONAS;

    //List of each zone's neighbors
    public List<int> LISTA_VECINOS_ZONA_1;
    public List<int> LISTA_VECINOS_ZONA_2;
    .
    .
    .
    public List<int> LISTA_VECINOS_ZONA_16;

    //List of lists of each zone's neighbors to optimize searchs
    public List<List<int>> LISTA_LISTAS_VECINOS;

    //The token object
    public Transform FICHA;

    public void MoverFicha(){
        Vector3 TP = FICHA.transform.position; 
        TP.x = this.transform.position.x;
        TP.y = this.transform.position.y;
        FICHA.transform.position = TP;
    }

    //Function to clarify the neighbors of the token at the momment
    public List<int> Vecinos(){
        for (int i=0; i<15; i++) {
            if (FICHA.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && FICHA.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
                return LISTA_LISTAS_VECINOS[i];
                }
        return LISTA_VECINOS_ZONA_1;
    }

    //Function to define the zone that got triggered by the click
    public int DefinirZonaClick(){
        for (int i=0; i<15; i++) {
            if (this.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && this.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
                return i+1;
                }
        return 1;
    }

    void Awake(){

        //Fill the list of zone objects
        LISTA_OBJ_ZONAS.Add (OBJ_ZONA_1);
        LISTA_OBJ_ZONAS.Add (OBJ_ZONA_2);
        .
        .
        .
        LISTA_OBJ_ZONAS.Add (OBJ_ZONA_16);

        //Clarify the neighbors of each zone
        LISTA_VECINOS_ZONA_1.Add (2);
        LISTA_VECINOS_ZONA_2.Add (1);
        .
        .
        .
        LISTA_VECINOS_ZONA_16.Add (15);

    }

    // Use this for initialization
    void Start () {

        //Fill the list of lists of neighbors of each zone
        LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_1);
        LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_2);
        .
        .
        .
        LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_16);
    }

    // Update is called once per frame
    void Update () {

        //Define the list of neighbors for the token at the momment of trigger
        List<int> VECINOS = Vecinos ();

        //Define the clicked zone
        int INT_ZONA_CLICK = DefinirZonaClick ();

        //If the triggered zone is in the token's neighbors list then it moves towards that zone
        if (VECINOS.Contains (INT_ZONA_CLICK))
                        MoverFicha ();

    }
}

This is my setup:

enter image description here

These are the errors I'm getting at the moment:

enter image description here

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
GMaiolo
  • 4,207
  • 1
  • 21
  • 37
  • 1
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Jan 11 '15 at 13:08

1 Answers1

0

You're declaring LISTA_VECINOS_ZONA_X etc. but you're not actually giving them a value/creating an instance of the object (or that's what I can deduce from the provided code). As a result, when you're adding them to LISTA_LISTAS_VECINOS, you're getting a NullReference error.

Create instances of LISTA_VECINOS_ZONA_X before adding them to the list at method Start().

Edit:

Your list of lists is declared in your code, but I don't see it actually being instantiated. This is what should be in the place of the declaration.

public List<List<int>> LISTA_LISTAS_VECINOS = new List<List<int>>();
Community
  • 1
  • 1
Gaessaki
  • 836
  • 1
  • 8
  • 16
  • LISTA_VECINOS_ZONA_X get it's items stated in the Awake() method, which is triggered before Start(), I'm getting the same error even when I add the items in Start() method before anything else. – GMaiolo Jan 11 '15 at 18:59
  • Could you show the code for line 154 of Moveitem.cs? – Gaessaki Jan 11 '15 at 22:49