- Home /
How to access/print parts of a list
I'm working on a script that checks a tile for its name and returns the value. That part works fine but I cannot figure out a way to compare that value to a list of tiles.
First, the TIle class that stores the properties of a tile.
public class Tile
{
private string name;
private int movementCost;
public string Name { get { return name; } set { name = value; } }
public int MovementCost { get { return movementCost; } set { movementCost = value; } }
public Tile(string name, int movementCost) // Constructor
{
name = Name;
movementCost = MovementCost;
}
}
Right now I only need the game and movementCost but I plan to add in some more functionality later. Then the tiles are initialised when a new game is started.
public class GameTiles : MonoBehaviour
{
public static List<Tile> gameTiles = new List<Tile>();
void Start ()
{
gameTiles.Add(new Tile("Walls", 250));
gameTiles.Add(new Tile("Decorations", 25));
gameTiles.Add(new Tile("Dirt", 15));
foreach (Tile tile in GameTiles.gameTiles)
{
//dunno
}
}
So the idea is that a raycast gets a name (string hitResult in another script) of a tile (that part works) and then searches the list of tiles for a match ( in the string Name). If a match is found the the script should also return the int movementCost.
In the end I realised that I even cannot figure out how to print out the tile list with Debug.Log, at best I get "Tile" (class name) with Debug.Log(tile); or null with Debug.Log(tile.Name); and similar methods. I only can see that the list size is correct by the amount of printed out items.
So, how do I compare a raycast result (string) with the string element of a list? And how do I simply print out the whole list just to see if it has initialised properly?
Answer by cbjunior · Jun 29, 2017 at 10:55 PM
You're misassigning the variables in your constructor. You need to change
name = Name;
movementCost = MovementCost;
To
this.name = name;
this.movementCost = movementCost;
Thats why you're getting a null result with Debug.Log (tile.Name)
. You arent being explicit with the scope and aren't ever actually assigning the variables in your script.
Oh I missed that. Good catch. Alternatively just let the C# compiler make these getters and setters and the backing fields for you, by declaring them as I showed at the end of my answer.
Thank you so much, I figured it was something like this since I couldn't get a bunch of list/linq methods working. Now I can get both values out of the list.
Not a problem my dude. Its an easy mistake to make. The worst ones are those kind where the error isnt where yoh think it is
Answer by Jwizard93 · Jun 29, 2017 at 10:52 PM
To print the list
foreach(Tile tile in GameTiles.gameTiles)
{
Debug.Log(tile.Name);
}
To test:
foreach(Tile tile in GameTiles.gameTiles)
{
if (tile.Name == someOtherString)
{
//do something
}
}
Also you are just currently using default getting and setting behavior so if that isnt changing save yourself the trouble:
public string name { get; set; }
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to sum a property of a class in a List 2 Answers
List .add problem 1 Answer
Need my function to work with different lists of different values (classes) 1 Answer
Displaying the players through GUI 1 Answer