• 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 Aug 11, 2017 at 04:41 PM by Igor_Vasiak for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Igor_Vasiak · Jun 02, 2017 at 12:22 PM · scripting problemarrayreflectionsystemfieldinfo

Trouble with fieldInfo.SetValue() and arrays

Hmmm... Again having trouble with SetValue... Well, since I didn't found anything about this in the web I've decided to ask it here:

I have this void, witch is:

 public void AddItem(Collider2D other, int amount)
 {
     int[] itemArray = (int[])GetType().GetField(other.GetComponent<Drops>().ItemType.ToString(), BindingFlags.Public | BindingFlags.Instance).GetValue(this);
     int id = other.GetComponent<Drops>().id;

     if (itemArray.Length > id)
         GetType().GetField(other.GetComponent<Drops>().ItemType.ToString()).SetValue(this, itemArray[id] + amount);
     else Debug.LogWarning("Help here! Not working at all! " + other.GetComponent<Drops>().id);
 }

It was supposed to detect two things when a player triggers with an item:

  public enum ItemType {basicMaterial, advancedMaterials};
  public class Drops : MonoBehaviour
  {
       int id; //If ItemType is equals to basicMaterial: 0 = Carbon, 1 = Copper...
  }

My only problem is after that. Once it is done detecting those things, it needs to ADD, lets say one, to an array. How does it knows where to add it? Easy. Using:

  GetType().GetField(Collider2D other.GetComponent<Drops>().ItemType.ToString());

I can easely find the array (since its name is equals to the ItemType (ex. array, ItemType: basicMaterials)), but I can't apply the value to it, sice I can't define at what point of the array's length I must add it. Any help would be amazing!

PS: After all that I still place that array inside another script, and then I save it to a .json file, but this I've already made and mastered.

I'm using fieldInfo.SetValue because I want my scripts to be as short as possible, and as readable as possible.

Another thing is that I call AddItem() inside of OnTriggerEnter2D() and set the Collider2D from AddItem() as the Collider2D from OnTriggerEnter2D().

Yes, I'm aware that I could just change the values individually for each array instead of trying to find it depending of witch ItemType is assigned and witch id is assigned, but I don't want my scripts to get overpopulated, so...

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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by ShadyProductions · Jun 02, 2017 at 01:11 PM

You should use a list for this, but if you really want to go for an array.

I suppose you could do something cheap using LINQ like this:

 var list = itemArray.ToList(); //convert array to a list
 list.Add(youritem); //add the value
 itemArray = list.ToArray(); //re-convert the list to an array
Comment
Add comment · Show 5 · 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 Igor_Vasiak · Jun 02, 2017 at 01:14 PM 0
Share

Actually, indexing an array's value to the "itemArray" isn't the problem. The problem is in how to add the value to the array that "itemArray" represents using "System.Reflection". To be honest with you, I never did fully understood the lists... Then I can't know if that could solve my issue.

By the way, how do I do something like this for a list?

  itemArray[someLengthPoint];
avatar image ShadyProductions Igor_Vasiak · Jun 02, 2017 at 01:22 PM 1
Share

Reflection is a bad thing to use because it is very performance taking, I really do not recommend using reflection. List's are the perfect solution to your problem. Use my linq statement it should work to what you are trying to achieve. It doesn't change an existing index, it add's a new index. To be honest, if each item has a certain ID, then I would just simply use a dictionary ins$$anonymous$$d of an array.

//not that the key is unique, so you can't have the same id

https://www.dotnetperls.com/dictionary

 Dictionary<int, Item> itemDictionary = new Dictionary<int, Item>();

then you can do something like this

 itemDictionary.Add(id, Item);

and to get a value by its Id:

 Item itemValue;
 if (itemDictionary.TryGetValue(id, out itemValue)) {
 //it found the item and it is referenced inside the itemValue variable
 }
 else {
 //it did not find your item by this id, and itemValue variable is null
 }
avatar image Igor_Vasiak ShadyProductions · Jun 02, 2017 at 01:37 PM 0
Share

Yeah, it kinda works, but still, how do I apply the "itemArray" to it's "parent" array? Just like I'm trying to do at line 7, inside AddItem(). (Still talking about the list thing, not the Dictionary one).

Show more comments

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

107 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DateTime.now not working on iOS 0 Answers

Shop system isn’t working and I don’t know why 1 Answer

Check if pressed key exists inside an array of KeyCodes 2 Answers

Unity Jobs Unknown Length of NativeArray 0 Answers

Trouble with adding value using FieldInfo.SetValue(this, int + int) 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges