Hello, the error is a null reference exception on the AddRange line. It seems to think that something there, either the List or one of the items being added, reference is null. The second script is one from one of the items being added. This is quite a simple issue but I can’t pin down the cause. Can anyone offer advice or spot the probably obvious problem that I’m failing to see? I can offer more information if you should need to request it.
Thanks.
public void deathRelist(GameObject nullShip)
{
//find list with null ship and store
List<GameObject> listWithNullShip = FindListContainingShip(nullShip);
//create empty list for storage
List<GameObject> newListForShips = null;
//remove null ship from list
listWithNullShip.Remove(nullShip);
//store remaining ships in new lists
newListForShips.AddRange(listWithNullShip);
//clears and removes old list from master list
listWithNullShip.Clear();
listOfShipLists.Remove(listWithNullShip);
//relists all ships in new list one ship at a time.
foreach(GameObject ship in newListForShips)
{
ship.transform.parent.GetComponent<GridSquare>().reList();
}
newListForShips.Clear();
}
}
SECOND SCRIPT
public void Destroy()
{
if(canDestroy == true)
{
Debug.Log ("Destroy called");
//stores the ship which has been destroyed
nullShip = this.gameObject;
//resets parents reference variables
this.transform.parent.GetComponent<GridSquare>().currentShip = null;
this.transform.parent.GetComponent<GridSquare>().shipDisplay = null;
this.transform.parent.GetComponent<GridSquare>().currentShipType = null;
this.transform.parent.GetComponent<GridSquare>().isEmpty = true;
//deparents ship from grid
this.transform.parent = null;
//gets reference for arraholder
ArrayHolder scriptRef = reference1.GetComponent<ArrayHolder>();
//Calls method to relist the ships in null ships list
scriptRef.deathRelist(nullShip);
//Destroys this ship.
Destroy(this.gameObject);
}
}
}
public void shipPlaced(int x, int y, GameObject gridShip, string gridShipType)
{
List<GameObject> shipList;
bool adjacentShipFound = false;
List<GameObject> lastListAddedTo = null;
foreach(GameObject item in gridArray) //Look at every spot in grid
{
GridSquare currentSquare = item.GetComponent<GridSquare>();
int x2,y2;
//Read x2 y2 from item name
x2 = currentSquare.X;
y2 = currentSquare.Y;
//check if currentSquare next to newly placed ship
if(x2 == x+1 && y2 == y || x2 == x-1 && y2 == y || x2 == x && y2 == y+1 || x2 == x && y2 == y-1)
{
//AND if it contains a ship of the same type
if(currentSquare.currentShipType == gridShipType)
{
//identified a ship which is adjacent to our newly placed ship
GameObject adjacentShip = currentSquare.currentShip;
//Find the list this found ship exists in already
List<GameObject> matchingShipList = FindListContainingShip(adjacentShip);
//Have we already found a matching list?
if (adjacentShipFound == false)
{
//Add newlyAddedShip to the matchingShipList
matchingShipList.Add(gridShip);
lastListAddedTo = matchingShipList;
adjacentShipFound = true;
}
else
{
//Combine last list and matching list together
lastListAddedTo.AddRange(matchingShipList);
listOfShipLists.Remove(matchingShipList);
}
}
}
}