Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NationalRedoubt · Jun 29, 2017 at 10:40 PM · listclass

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jwizard93 · Jun 29, 2017 at 10:57 PM 1
Share

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.

avatar image NationalRedoubt · Jun 29, 2017 at 11:14 PM 0
Share

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.

avatar image cbjunior NationalRedoubt · Jun 29, 2017 at 11:21 PM 0
Share

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

avatar image
0

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; }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges