- Home /
How to fix this "MissingMethodException"
I got 2 Scripts (one on my player GameObject and one on a shoppingNPC GameObject). I have the same class to store some properties declared in bot scripts:
[System.Serializable]
public class Item{
public string name;
public int costs;
public float _value;
public string itemClass;
public int slotNumber;
public Texture2D texture;
}
In my "Inventory" script (the one on the player) i have a function to buy an item which takes "Item _item" as a paramater. I want to call this function from the other script via "SendMessage".
This is the function in the inventory script:
void BuyItem(Item _item){
if(_item.costs < rubyAmount){
}
else{
infoText.SendMessage("ShowText", "I need more Rubys...");
}
}
This is the function with the SendMessage:
void OnGUI(){
if(shopActive){
GUI.skin = shopSkin;
GUI.BeginGroup(menuAreaNormalized);
int buttonNumber = 0;
for(int i = 0; i < _itemClass.Length; i++){
for(int k = 0; k < _itemClass[i].buyableItem.Length; k++){
if(_itemClass[i].buyableItem[k].buyable){
if(GUI.Button(new Rect(firstButton.x,firstButton.y + 48 * buttonNumber, firstButton.width, firstButton.height), _itemClass[i].buyableItem[k].itemGUI)){
Debug.Log("Classnummer: " + i.ToString() + "; Itemnummer: " + k.ToString() + " pressed");
player.SendMessage("BuyItem", _itemClass[i].buyableItem[k]._item);
}
buttonNumber++;
}
}
}
GUI.EndGroup();
}
}
_itemClass is an class storing the className and the item to buy. buyableItem is a class which stores all information needed to buy the item in the shop including the _item of type "Item" (the class from the beginning).
I can't find an error but if I click on an item in the shop it says "MissingMethodException: The best match for method has some invalid parameter."
Anyone can help me?
I'm not totally sure what the issue is; I see you call player.Send$$anonymous$$essage; is "player" a GameObject, $$anonymous$$onobehavior, or what? For Send$$anonymous$$essage to work, the Inventory component needs to be on the same GameObject as "player". If it is a child or something, you should use Broadcast$$anonymous$$essage.
I'm not much of a fan of Send$$anonymous$$essage because you don't know if it will work until runtime; since you already have the "player" object, my preferred calling method would be something like this:
Inventory inv = player.GetComponent<Inventory>();
if(inv != null)
{
inv.BuyItem(_itemClass[i].buyableItem[k]._item);
}
So, I guess my solution would be to fix your Send$$anonymous$$essage problem by not using Send$$anonymous$$essage ;).
I had a similar issue in my project and the issue turned out to be ambiguity with the method name of a public method in another manager class. Both Classes had a Load$$anonymous$$ap() method and changing the name of the notification was the only way I could get rid of the error
Answer by aldonaletto · May 11, 2013 at 09:26 PM
This error means that you're calling a function with the wrong type or number of arguments. Take a look at the line where the error occurs and check the function calls to find which one has an incompatible argument.
Hmm I already checked it some times but haven't found the issue. I guess the problem is that the "Item" class which I am using as a parameter isn't accepted. I just declared the same "Item" class in both scripts but it seems that it has to be exactly the same class..