- Home /
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
}
}
}
}
}
Remove the 'new' from this line:
new shipList.Add(gridShip);//creates new ship list and adds ship to it
Answer by Jamora · Feb 24, 2014 at 10:17 PM
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();
He does not need the
shipList = new List<GameObject>();
as he already does that on line 7.
The OP clearly wants to clear the list. Creating a new List will have that effect.
If the list is not intended to be cleared, then removing the new
- like you suggested - will do the trick.
I would like to have multiple instances of the List,to store lots or groups of ships, but im not sure thats possible with lists. whats the most reliable alternative?
I just can shake this feeling that there is a better way to achieve what is tried to be done here. Have you read this page?
Regardless, having a list of lists is possible: you declare it by
List<List<GameObject>> allShipLists = new List<List<GameObject>>();
Now, adding to, e.g. the first list is achieved by
allShipLists[0].Add(gridship);
You also mention you want to find which list a specific GameObject belings to. That is done by iterating through all element with for-loops:
for(int i=0;i<allShipLists.Count;i++){
for(int j=0;j<allShipLists[i].Count;j++){
//check for equality here
}
}
You might also consider using Dictionaries. Using Dictionaries allows you to 'name' your lists, which you then access by this 'name'.
Dictionary<string, List<GameObject>> allShipLists = new Dictionary<string, List<GameObject>>();
//you then add a new list, named Group1 by
allShipLists.Add("Group1",new List<GameObject>());
//you add the gridShip to Group1 by
allShipLists["Group1"].Add(gridShip);
Thanks a lot Jamora, the information and links you provided where most helpful.