- Home /
The question is answered, right answer was accepted
Trouble with adding value using FieldInfo.SetValue(this, int + int)
I'm creating a simple inventory script for my game. When the player collides (using OnTriggerEnter2D) with the item (let's say it is a int Iron) it checks for the name of the item, so the inventory knows what to do next. Then I add the item to a int (int Iron) USING THE NAME OF THE ITEM DROP (witch in this case is Iron).
My script is meant to be as clean and as readable as possible, so I did this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;
public class PItems : MonoBehaviour
{
[SerializeField]private string[] itemTags;
//Basic Materials
public int Carbon;
public void AddItem(Collider2D other, int amount)
{
int item = (int)this.GetType().GetField(other.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).GetValue(this);
(int)GetType().GetField(other.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).SetValue(this, item + amount);
Debug.Log(Carbon);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (itemTags.Contains(other.name)) {
AddItem(other,1);
Debug.Log(Carbon);
}
}
}
But Visual Studio keeps telling me at AddItem that I cant convert a void into a integer. But where the hell am I converting things in here (I mean, I know witch line, but I don’t know where IN the line)? I just want it to work, and IT DOESN'T! Why things in Unity doesn't just work as they're meant to be?!
At
(int)GetType().GetField(other.name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic).SetValue(this, item + amount);
You are converting to an integer.
Why you make things so complicating, just make an Item object that is not derived from $$anonymous$$onoBehaviour and an Inventory class that has the AddItem method which you pass your item to.
@ShadyProductions Since I have multiple items and I’m not that expert with C# I have chosen to do it this way. But if you could show me another method that uses ONLY ONE script as short as $$anonymous$$e I wold thanks, ‘cause I don’t know how to do this method you said. Actually, I would like to know if there is any way to make Visual Studio and Unity accept this and not get the conversion error, but still working with integers. But anyway, thanks for giving your time to answer me.
Answer by Igor_Vasiak · May 29, 2017 at 02:31 PM
Ok, besides my incredible ignorance, my error was just that (int) at line 17. That was the mistake. Have taken a while to figure it out, but now it's working propperly. The worst thing is that there's no answer to these cases at any place on the web. But, whatever, I already have figured it out.
That's because it's basic C# knowledge, not unity specific.
Exactly. The signature of the SetValue method is well documented. As you can see the method doesn't return any value (void). Therefore you can't cast the return value to "int".