Problems creating new List of type GameObject and adding GameObject to it.

Hey,

I’m trying to create a new List (the shipList) whenever the shipPlaced function goes to ‘else’, and then placing ‘currentShip’ into it. The object gridShip refers to a GameObject in another script (which is sent over when the other script refers to shipPlaced. The other script would be various instances of ‘GridSquare’). gridShip cannot be null when the method is called. x and y are the co-ordinates on a grid, and refer to the individual spot the method is called from. The error I am receiving in full is:

Assets/Scripts/ArrayHolder.cs(34,45): error CS0118: ArrayHolder.shipList' is a field’ but a `type’ was expected.

Should I be using arraylists or arrays instead? Or is there another problem? I would like to be able to dynamically create new Lists as well as change the length of them.
Additionally, I would like to have a way of finding the List an object is in from only having the object itself. Is this possible?

Thanks in advance.

using UnityEngine;
using System.Collections.Generic;

public class ArrayHolder : MonoBehaviour {
	
	GameObject[] gridArray = new GameObject[64];
	List<GameObject> shipList = new List<GameObject>();
	

	void Start ()
	{
		//need to create and add grid spots to gridArray
	}
	
	void Update () {
	
	}
	
	public void shipPlaced(int x, int y, GameObject gridShip)
	{
		foreach(GameObject item in gridArray)
		{
			if(x == x+1 && y == y || x == x-1 && y == y || x == x && y == y+1 || x == x && y == y-1)
			{
				if(item.GetComponent<GridSquare>().currentShip == gridShip)
				{
					
				}
				
				else
				{
					new shipList.Add(gridShip);//creates new ship list and adds ship to it
				}
			}
		}			
	}
}

What you’re trying to do is create a new field, like the error says. What you need to be doing is create a new list, then add a gameobject to that.

So, instead of your line 32, you need to

shipList = new List<GameObject>();
shipList.Add(gridShip);

Alternatively, instead of creating a new list, you could also just empty the existing list with shipList.Clear();