The Best Overloaded Method Match Has Some Invalid Arguments

First, I apologize for making a necro thread. However, the answer for that thread didn’t work, as it had already been implemented in the code, yet I was still getting the same error. I’ve tried Google, I’ve tried crawling around the forums, and I’ve tried every possible solution I can think of, but to no avail.

Here’s the code.

using UnityEngine;
using System.Collections;
using System;	//added for the enum class

public class CharacterGenerator : MonoBehaviour {
	private PlayerCharacter _toon;
	private const int STARTING_POINTS = 30;
	private const int MIN_STARTING_ATTRIBUTE_VALUE = 10;
	private int pointsLeft;

	private const int OFFSET = 10;
	private const int LINE_HEIGHT = 20;

	private const int STAT_LABEL_WIDTH = 100;
	private const int BASEVALUE_LABEL_WIDTH = 30;
	private const int BUTTON_WIDTH = 20;
	private const int BUTTON_HEIGHT = 20;

	private int statStartingPos = 40;

	//use this for initialization
	void Start(){
		_toon = new PlayerCharacter{};
		_toon.Awake();

		pointsLeft = STARTING_POINTS;

		for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
			_toon.GetPrimaryAttribute(cnt).BaseValue = MIN_STARTING_ATTRIBUTE_VALUE ;
		}
		_toon.StatUpdate ();
	}

	//update is called once per frame
	void Update(){

	}

	void OnGUI() {
		DisplayName ();
		DisplayPointsLeft ();
		DisplayVitals ();
		DisplayAttributes ();
		DisplaySkills ();
	}

	private void DisplayName() {
		GUI.Label(new Rect(10, 10, 50, 25), "Name: ");
		_toon.Name = GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
	}

	private void DisplayAttributes(){
		for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
			GUI.Label(new Rect(OFFSET, //x
			                   statStartingPos + (cnt * LINE_HEIGHT), 	//y
			                   STAT_LABEL_WIDTH,	//width
			                   LINE_HEIGHT), 	//height
			          ((AttributeName)cnt), ToString());

			GUI.Label (new Rect(STAT_LABEL_WIDTH + OFFSET,	//x
			                    statStartingPos + (cnt * LINE_HEIGHT),	//y
			                    BASEVALUE_LABEL_WIDTH, 	//width
			                    LINE_HEIGHT), 	//height
			           _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());

			if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH, 	//x
			                       statStartingPos + (cnt * BUTTON_HEIGHT),	//y
			                       BUTTON_WIDTH,	//width
			                       BUTTON_HEIGHT), 	//height
			              "-")) {
				if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
					_toon.GetPrimaryAttribute(cnt).BaseValue--;
					pointsLeft++;
					_toon.StatUpdate ();
				}
			}
			if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH,	//x
			                       statStartingPos + (cnt * BUTTON_HEIGHT),	//y
			                       BUTTON_WIDTH,	//width
			                       BUTTON_HEIGHT),	//height
			              "+")){
				if(pointsLeft > 0) {
					_toon.GetPrimaryAttribute(cnt).BaseValue++;
					pointsLeft--;
					_toon.StatUpdate ();
				}
			}
		}
	}

	private void DisplayVitals(){
		for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
			GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt), ToString());
			          GUI.Label (new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 7) * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());
		}
	}

	private void DisplaySkills(){
		for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
					GUI.Label(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2, statStartingPos + (cnt * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt), ToString());
					GUI.Label (new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH * 2 + OFFSET * 2 + STAT_LABEL_WIDTH, statStartingPos + (cnt * 25), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetSkill(cnt).AdjustedBaseValue.ToString());
		}
	}

	private void DisplayPointsLeft(){
		GUI.Label (new Rect (250, 10, 100, 25), "Points: " + pointsLeft.ToString());
	}
}

As for the errors…

Line - 54
The best overloaded method match for ‘UnityEngine.GUI.Label(UnityEngine.Rect, string, UnityEngine.GUIStyle)’ has some invalid arguments

Line - 58
Argument ‘2’: cannot convert from ‘AttributeName’ to ‘string’

Line - 93
) expected

Line - 100 [2 errors]
#1 The best overloaded method match for ‘UnityEngine.GUI.Label(UnityEngine.Rect, string, UnityEngine.GUIStyle)’ has some invalid arguments
#2 Argument ‘2’: cannot convert from ‘VitalName’ to ‘string’

So what am I doing wrong here?

 GUI.Label(new Rect(OFFSET, //x

                               statStartingPos + (cnt * LINE_HEIGHT),   //y

                               STAT_LABEL_WIDTH,    //width

                               LINE_HEIGHT),    //height

                      ((AttributeName)cnt), ToString());

theres a comma instead of a period at line 9 (58 in your code)
Since that means it is no longer a string, it no longer is a valid entry to a GUI label, so it is throwing an error.

1 Like

First off this is an issue.

Line 58

((AttributeName)cnt), ToString());

Shouldnt it be

((AttributeName)cnt).ToString());

Beaten! :wink:

Hi Demon,

please have a look at this page:

at it´s top you can see the overloaded methods for GUI.Label. The Last parameter must not be a string.
Only if you just use 2 params (Rect for position and String for the text) then the last parameter is a string.

I think you did not want to make an comma in front of your “ToString()” methods.

I replaced the commas from the other lines of code with a period (as well as that one), saved it and ran the debug, but it leaves me with one error.

“) expected” from line 93.

Thanks for catching that, by the way. I figured it was something trivial, and that it would help if a fresh pair of eyes took a look at it. Like proofreading.

The following (a few times in your code):

((AttributeName)cnt), ToString();

should be

((AttributeName)cnt.)ToString();

And you’re also missing a closing bracket in line 93, just as it say in the error messages.

line 93 has 13 total “(” and “)”. Thats not an even number, so something is missing.

he is correct, just need to cap it off with one more parentheses

Tried it, gave me 3 errors.

) expected
; expected
Invalid expression term ‘)’

Okay, got it.

Line 93 should read as…

GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT)), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt).ToString());

instead of…

GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt), ToString());

And… now I’m getting this when I try to run it.

NullReferenceException: Object reference not set to an instance of an object
CharacterGenerator.DisplayVitals () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:94)
CharacterGenerator.OnGUI () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:42)

Um, did you write this code? I ask because this is some fairly basic debugging and it might be better to explain what the errors mean than just point out the answer.

The best overloaded method match for 'UnityEngine.GUI.Label(UnityEngine.Rect, string, UnityEngine.GUIStyle)' has some invalid arguments

This error is telling you that a parameter you are trying to send to a function is incorrect. You should check each parameter you are sending a be sure it is of the correect type.

Argument '2': cannot convert from 'AttributeName' to 'string'

Cannot canvert means you have some object, in this case “AttributeName” which you are attempting to change into something it is not in this case a “string”. This can occur in various places, when setting one thing = to another or when passing a parameter incorrectly. This means your function call to GUI.Label and line 54 was generating 2 different errors. It even points out which parameter is hte problem, “Argument ‘2’”.

) expected

Expected is fairly simple, the compiler expected something, in this case it expected a “)”. Find where its missing and add it.

Like I said, I figured those out. Refer to my previous post, as I received a new error when attempting to run the game, although no errors are registered in the compiler.

NullReferenceException: Object reference not set to an instance of an object
BaseCharacter.SetupVitalModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:91)
BaseCharacter.SetupVitals () (at Assets/Scripts/Character Classes/BaseCharacter.cs:63)
BaseCharacter.Awake () (at Assets/Scripts/Character Classes/BaseCharacter.cs:24)
CharacterGenerator.Start () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:24)

NullReferenceException: Object reference not set to an instance of an object
CharacterGenerator.DisplayVitals () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:94)
CharacterGenerator.OnGUI () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:42)

And here’s the code for BaseCharacter

using UnityEngine;
using System.Collections;
using System;	//added to access the enum class

public class BaseCharacter : MonoBehaviour {
	private string _name;
	private int _level;
	private uint _freeExp;

	private Attribute[] _primaryAttribute;
	private Vital[] _vital;
	private Skill[] _skill;

	public void Awake() {
		_name = string.Empty;
		_level = 0;
		_freeExp = 0;

		_primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
		_vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
		_skill = new Skill[Enum.GetValues (typeof(SkillName)).Length];

		SetupPrimaryAttributes ();
		SetupVitals ();
		SetupSkills ();
	}

	public string Name {
		get{ return _name; }
		set{ _name = value; }
		}

	public int Level {
		get{ return _level; }
		set{ _level = value; }
		}

	public uint FreeExp {
		get{ return _freeExp; }
		set{ _freeExp = value; }
		}

	public void AddExp(uint exp) {
		_freeExp += exp;

		CalculateLevel ();
		}

	//take avg of all of the player's skills and assign that as the player level
	public void CalculateLevel() {

	}
	private void SetupPrimaryAttributes() {
		for (int cnt = 0; cnt < _primaryAttribute.Length; cnt++) {
			_primaryAttribute[cnt] = new Attribute();
		}
	}

	private void SetupVitals() {
		for (int cnt = 0; cnt < _vital.Length; cnt++) {
			_vital[cnt] = new Vital();

			SetupVitalModifiers();
		}
	}

	private void SetupSkills() {
		for (int cnt = 0; cnt < _skill.Length; cnt++) {
			_skill[cnt] = new Skill();

			SetupSkillModifiers();
		}
	}

	public Attribute GetPrimaryAttribute(int index) {
		return _primaryAttribute [index];
	}

	public Vital GetVital(int index) {
		return _vital [index];
	}

	public Skill GetSkill(int index) {
		return _skill [index];
	}

	private void SetupVitalModifiers() {
		//health
		GetVital ((int)VitalName.Health).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), 1f));
		//energy
		GetVital ((int)VitalName.Energy).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), 1f));
		//mana
		GetVital ((int)VitalName.Mana).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), 1f));
	
	}

	private void SetupSkillModifiers() {
		//melee offense
		GetSkill ((int)SkillName.Melee_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Strength), .33f));
		GetSkill ((int)SkillName.Melee_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
		//melee defense
		GetSkill ((int)SkillName.Melee_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), .33f));
		GetSkill ((int)SkillName.Melee_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Willpower), .33f));
		//magic offense
		GetSkill ((int)SkillName.Magic_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), .33f));
		GetSkill ((int)SkillName.Magic_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));
		//magic defense
		GetSkill ((int)SkillName.Magic_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Willpower), .33f));
		GetSkill ((int)SkillName.Magic_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));
		//ranged offense
		GetSkill ((int)SkillName.Ranged_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
		GetSkill ((int)SkillName.Ranged_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), .33f));
		//ranged defense
		GetSkill ((int)SkillName.Ranged_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
		GetSkill ((int)SkillName.Ranged_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));

	}

	public void StatUpdate() {
		for (int cnt = 0; cnt < _vital.Length; cnt++)
			_vital [cnt].Update ();

		for (int cnt = 0; cnt < _vital.Length; cnt++)
			_skill [cnt].Update ();
	}


}

This is a call stack, might make more sense to read from bottom to top. It tells you where the error was called from. The first function it called was “CharacterGenerator.OnGUI ()”, from there it called “CharacterGenerator.DisplayVitals ()”, this is where it died and found a null reference. A null lreference is basically a variable without data or a variable where A == null is true. So if you look at the line where the error occured, in this case line “94” we have this…

   GUI.Label (new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 7) * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());

Something in this function is null. Its unlikely for OFFSET, STAT_LABEL_WIDTH, LINE_HEIGHT, BASEVALUE_LABEL_WIDTH, statStartingPos to be null, you have defined them at the top of the class. This only leaves

_toon.GetVital(cnt).AdjustedBaseValue.ToString()

_toon is defined in start, so this isnt the issue, cnt is defined in the loop so the issue is either in the return value from GetVital(cnt) or whatever AdjustedBaseValue returns, without seeing the PlayerCharacter its not possible for me to diagnose. I have a sneaky suspiscion that cnt is a value that GetVital doesnt understand and is returning null. HArd to say for sure though.

I edited the post to include the BaseCharacter code, just in case. Will add a new post with PlayerCharacter code.

public class PlayerCharacter : BaseCharacter {
	// Use this for initialization
	void Start () {
	}
	// Update is called once per frame
	void Update () {
	}
}

CharacterGenerator.Start

        _toon = new PlayerCharacter{};

should be

        _toon = new PlayerCharacter();

use ( ) not { }

might be the issue

It fixed one issue, but I’m still left with the BaseCharacter null.

NullReferenceException: Object reference not set to an instance of an object
BaseCharacter.SetupVitalModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:91)
BaseCharacter.SetupVitals () (at Assets/Scripts/Character Classes/BaseCharacter.cs:63)
BaseCharacter.Awake () (at Assets/Scripts/Character Classes/BaseCharacter.cs:24)
CharacterGenerator.Start () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:24)

The offending line.

GetVital ((int)VitalName.Energy).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), 1f));

In order to track down these issues its sometimes helpful to break it apart and narrow down the suspects. You can always put it back into a compant form once the issue is resolved.

Vital vit = GetVital ((int)VitalName.Energy);
Attribute att = GetPrimaryAttribute ((int)AttributeName.Endurance);
ModifyingAttribute mod = new ModifyingAttribute (att, 1f);
vit.AddModifier (mod);

See which line is truly throwing the error.