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 /
This question was closed Oct 20, 2020 at 04:28 PM by I_Am_Err00r for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by I_Am_Err00r · Nov 04, 2018 at 04:03 PM · arrayslistsforeachwhile-loopindexof

How to find x item in list?

I know I'm not the first person to ask this, but I was hoping to get a little extra and custom support because I have spent a few hours on this and am not getting any closer, running out of ideas, and want to see if a kind developer out there could take a few minutes to explain how Lists actually work.

The short story is I have a player attribute system and I'm trying to get the value of each individual attribute and tie them to their respective values (strength does more damage, health gives more health, etc).

The issue is my code uses scriptable objects to create each attribute and the length of this list and values of each attribute are setup through the inspector (not in the script itself), and I'm trying to find a way to specify each individual attribute and grab the value of that attribute so I can assign it to it's respective skill or stat.

Here is a screenshot of what my list looks like in the inspector: alt text

I am able to get a foreach loop to display the vaules of these attributes at start with this code:

 private void Start()
 {
      foreach (PlayerAttributes attribute in Attributes)
 {
      Debug.Log(" " + attribute.attribute, attribute.amount);
       }
 }

This is giving me each of the Attribute names as they appear in order from the inspector, and also gives the right value of that Attribute as well: alt text

So my million dollar very simple question is how would I do the same thing, but only access say the third attribute (Accuracy)?

I want to be able to grab the value of Accuracy and then have that increase or decrease the weapon's range stat, so as accuracy grow, so does the range of that weapon, and of course this Accuracy attribute would affect the range stat of every weapon the player uses.

This seems like something that should be very easy to index or access, but I'm having a ton of trouble understanding this and all the tips I see online form their list in the code the are trying to reference, and not have the list form and set values of through the inspector.

Thanks in advance for your help!

console.png (20.1 kB)
attributes.png (19.6 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by JVene · Nov 04, 2018 at 04:30 PM

Both arrays and List generics are accessed with the [] operator, as in:

 PlayerAttributes a = Attributes[ 3 ];

Also, the members can be access without using a reference, as in:

 var n = Attributes[ 3 ].amount;


Comment
Add comment · Show 1 · 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 I_Am_Err00r · Nov 04, 2018 at 04:42 PM 1
Share

Thanks for the super quick response, I knew this was way easier than I was making it! I can't believe how hard I was making this!

I really appreciate the brief explanation rather than just pasting API documentation.

avatar image
1

Answer by Iarus · Nov 04, 2018 at 05:12 PM

Just be careful here, Attributes[3] means the 4th element. In C#, arrays and lists are zero based.


Also if the element at the requested index is null or the list has less elements than you're expecting, this will throw an IndexOutOfBoundsException or something like that. You have to either make sure (absolutely 100% sure) that the list will always be created with all the elements the code is expecting, or validate that Attributes.Length >= 4 before accessing Attributes[3].


Also if you create an other character later and the attributes are not in the same order, you might be expecting Accuracy , but maybe it'll be Strength. Then you would need to:

  • enforce the order in the list

  • or search for the correct attribute, either everytime you need it or cache it's index during initialization.

  • or maybe use a dictionary instead

  • or maybe all your heroes and ennemies will all have the same set of attributes and you could replace the list of attributes with an accuracy property and a strength property


It all depends what your game needs.

Comment
Add comment · Show 1 · 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 I_Am_Err00r · Nov 04, 2018 at 08:46 PM 0
Share

Thanks for your tips and feedback! The coding community is so helpful and tips like this really help me learn and reinforce good habits.

Follow this Question

Answers Answers and Comments

97 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 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

Returning one dimension of a two-dimensional array 1 Answer

how do I move array objects to world coordinates? 1 Answer

What is the maximum amount of objects that can be stored in an Array or List without causing any error? 1 Answer

Problems with PlayerPrefs or something else 1 Answer

Displaying a List or an Array in Editor file 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