Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

The Best Overloaded Method Match Has Some Invalid Arguments

Discussion in 'Scripting' started by EatUrDemon, May 22, 2014.

  1. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    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.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;   //added for the enum class
    5.  
    6. public class CharacterGenerator : MonoBehaviour {
    7.     private PlayerCharacter _toon;
    8.     private const int STARTING_POINTS = 30;
    9.     private const int MIN_STARTING_ATTRIBUTE_VALUE = 10;
    10.     private int pointsLeft;
    11.  
    12.     private const int OFFSET = 10;
    13.     private const int LINE_HEIGHT = 20;
    14.  
    15.     private const int STAT_LABEL_WIDTH = 100;
    16.     private const int BASEVALUE_LABEL_WIDTH = 30;
    17.     private const int BUTTON_WIDTH = 20;
    18.     private const int BUTTON_HEIGHT = 20;
    19.  
    20.     private int statStartingPos = 40;
    21.  
    22.     //use this for initialization
    23.     void Start(){
    24.         _toon = new PlayerCharacter{};
    25.         _toon.Awake();
    26.  
    27.         pointsLeft = STARTING_POINTS;
    28.  
    29.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
    30.             _toon.GetPrimaryAttribute(cnt).BaseValue = MIN_STARTING_ATTRIBUTE_VALUE ;
    31.         }
    32.         _toon.StatUpdate ();
    33.     }
    34.  
    35.     //update is called once per frame
    36.     void Update(){
    37.  
    38.     }
    39.  
    40.     void OnGUI() {
    41.         DisplayName ();
    42.         DisplayPointsLeft ();
    43.         DisplayVitals ();
    44.         DisplayAttributes ();
    45.         DisplaySkills ();
    46.     }
    47.  
    48.     private void DisplayName() {
    49.         GUI.Label(new Rect(10, 10, 50, 25), "Name: ");
    50.         _toon.Name = GUI.TextField(new Rect(65, 10, 100, 25), _toon.Name);
    51.     }
    52.  
    53.     private void DisplayAttributes(){
    54.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++){
    55.             GUI.Label(new Rect(OFFSET, //x
    56.                                statStartingPos + (cnt * LINE_HEIGHT),   //y
    57.                                STAT_LABEL_WIDTH,    //width
    58.                                LINE_HEIGHT),    //height
    59.                       ((AttributeName)cnt), ToString());
    60.  
    61.             GUI.Label (new Rect(STAT_LABEL_WIDTH + OFFSET,  //x
    62.                                 statStartingPos + (cnt * LINE_HEIGHT)//y
    63.                                 BASEVALUE_LABEL_WIDTH,  //width
    64.                                 LINE_HEIGHT),   //height
    65.                        _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
    66.  
    67.             if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH,   //x
    68.                                    statStartingPos + (cnt * BUTTON_HEIGHT), //y
    69.                                    BUTTON_WIDTH,    //width
    70.                                    BUTTON_HEIGHT),  //height
    71.                           "-")) {
    72.                 if(_toon.GetPrimaryAttribute(cnt).BaseValue > MIN_STARTING_ATTRIBUTE_VALUE) {
    73.                     _toon.GetPrimaryAttribute(cnt).BaseValue--;
    74.                     pointsLeft++;
    75.                     _toon.StatUpdate ();
    76.                 }
    77.             }
    78.             if(GUI.Button(new Rect(OFFSET + STAT_LABEL_WIDTH + BASEVALUE_LABEL_WIDTH + BUTTON_WIDTH,    //x
    79.                                    statStartingPos + (cnt * BUTTON_HEIGHT), //y
    80.                                    BUTTON_WIDTH,    //width
    81.                                    BUTTON_HEIGHT)//height
    82.                           "+")){
    83.                 if(pointsLeft > 0) {
    84.                     _toon.GetPrimaryAttribute(cnt).BaseValue++;
    85.                     pointsLeft--;
    86.                     _toon.StatUpdate ();
    87.                 }
    88.             }
    89.         }
    90.     }
    91.  
    92.     private void DisplayVitals(){
    93.         for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++){
    94.             GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt), ToString());
    95.                       GUI.Label (new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 7) * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());
    96.         }
    97.     }
    98.  
    99.     private void DisplaySkills(){
    100.         for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++){
    101.                     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());
    102.                     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());
    103.         }
    104.     }
    105.  
    106.     private void DisplayPointsLeft(){
    107.         GUI.Label (new Rect (250, 10, 100, 25), "Points: " + pointsLeft.ToString());
    108.     }
    109. }
    110.  
    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?
     
  2. Ilseroth

    Ilseroth

    Joined:
    May 18, 2014
    Posts:
    17
    Code (csharp):
    1.  GUI.Label(new Rect(OFFSET, //x
    2.  
    3.                                statStartingPos + (cnt * LINE_HEIGHT),   //y
    4.  
    5.                                STAT_LABEL_WIDTH,    //width
    6.  
    7.                                LINE_HEIGHT),    //height
    8.  
    9.                       ((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.
     
    rpeer likes this.
  3. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    First off this is an issue.

    Line 58
    Code (csharp):
    1. ((AttributeName)cnt), ToString());
    Shouldnt it be
    Code (csharp):
    1. ((AttributeName)cnt).ToString());
    Beaten! ;)
     
  4. MysteriX

    MysteriX

    Joined:
    Apr 8, 2014
    Posts:
    54
    Hi Demon,

    please have a look at this page:

    http://docs.unity3d.com/Documentation/ScriptReference/GUI.Label.html

    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.
     
  5. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    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.
     
  6. Suddan

    Suddan

    Joined:
    Jan 12, 2013
    Posts:
    21
    The following (a few times in your code):

    Code (csharp):
    1.  
    2. ((AttributeName)cnt), ToString();
    3.  
    should be

    Code (csharp):
    1.  
    2. ((AttributeName)cnt.)ToString();
    3.  
    And you're also missing a closing bracket in line 93, just as it say in the error messages.
     
  7. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    line 93 has 13 total "(" and ")". Thats not an even number, so something is missing.
     
  8. Ilseroth

    Ilseroth

    Joined:
    May 18, 2014
    Posts:
    17
    he is correct, just need to cap it off with one more parentheses
     
  9. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Tried it, gave me 3 errors.

    ) expected
    ; expected
    Invalid expression term ')'
     
    Last edited: May 22, 2014
  10. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Okay, got it.

    Line 93 should read as...
    Code (csharp):
    1.  
    2. GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT)), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt).ToString());
    3.  
    instead of...
    Code (csharp):
    1.  
    2. GUI.Label(new Rect(OFFSET, statStartingPos + ((cnt + 7 * LINE_HEIGHT), STAT_LABEL_WIDTH, LINE_HEIGHT), ((VitalName)cnt), ToString());
    3.  
     
  11. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    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)
     
  12. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    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.


    Code (csharp):
    1. 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.

    Code (csharp):
    1. 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'".

    Code (csharp):
    1. ) expected
    Expected is fairly simple, the compiler expected something, in this case it expected a ")". Find where its missing and add it.
     
  13. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    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.
     
  14. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37

    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

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System;   //added to access the enum class
    5.  
    6. public class BaseCharacter : MonoBehaviour {
    7.     private string _name;
    8.     private int _level;
    9.     private uint _freeExp;
    10.  
    11.     private Attribute[] _primaryAttribute;
    12.     private Vital[] _vital;
    13.     private Skill[] _skill;
    14.  
    15.     public void Awake() {
    16.         _name = string.Empty;
    17.         _level = 0;
    18.         _freeExp = 0;
    19.  
    20.         _primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
    21.         _vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
    22.         _skill = new Skill[Enum.GetValues (typeof(SkillName)).Length];
    23.  
    24.         SetupPrimaryAttributes ();
    25.         SetupVitals ();
    26.         SetupSkills ();
    27.     }
    28.  
    29.     public string Name {
    30.         get{ return _name; }
    31.         set{ _name = value; }
    32.         }
    33.  
    34.     public int Level {
    35.         get{ return _level; }
    36.         set{ _level = value; }
    37.         }
    38.  
    39.     public uint FreeExp {
    40.         get{ return _freeExp; }
    41.         set{ _freeExp = value; }
    42.         }
    43.  
    44.     public void AddExp(uint exp) {
    45.         _freeExp += exp;
    46.  
    47.         CalculateLevel ();
    48.         }
    49.  
    50.     //take avg of all of the player's skills and assign that as the player level
    51.     public void CalculateLevel() {
    52.  
    53.     }
    54.     private void SetupPrimaryAttributes() {
    55.         for (int cnt = 0; cnt < _primaryAttribute.Length; cnt++) {
    56.             _primaryAttribute[cnt] = new Attribute();
    57.         }
    58.     }
    59.  
    60.     private void SetupVitals() {
    61.         for (int cnt = 0; cnt < _vital.Length; cnt++) {
    62.             _vital[cnt] = new Vital();
    63.  
    64.             SetupVitalModifiers();
    65.         }
    66.     }
    67.  
    68.     private void SetupSkills() {
    69.         for (int cnt = 0; cnt < _skill.Length; cnt++) {
    70.             _skill[cnt] = new Skill();
    71.  
    72.             SetupSkillModifiers();
    73.         }
    74.     }
    75.  
    76.     public Attribute GetPrimaryAttribute(int index) {
    77.         return _primaryAttribute [index];
    78.     }
    79.  
    80.     public Vital GetVital(int index) {
    81.         return _vital [index];
    82.     }
    83.  
    84.     public Skill GetSkill(int index) {
    85.         return _skill [index];
    86.     }
    87.  
    88.     private void SetupVitalModifiers() {
    89.         //health
    90.         GetVital ((int)VitalName.Health).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), 1f));
    91.         //energy
    92.         GetVital ((int)VitalName.Energy).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), 1f));
    93.         //mana
    94.         GetVital ((int)VitalName.Mana).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), 1f));
    95.    
    96.     }
    97.  
    98.     private void SetupSkillModifiers() {
    99.         //melee offense
    100.         GetSkill ((int)SkillName.Melee_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Strength), .33f));
    101.         GetSkill ((int)SkillName.Melee_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
    102.         //melee defense
    103.         GetSkill ((int)SkillName.Melee_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), .33f));
    104.         GetSkill ((int)SkillName.Melee_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Willpower), .33f));
    105.         //magic offense
    106.         GetSkill ((int)SkillName.Magic_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), .33f));
    107.         GetSkill ((int)SkillName.Magic_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));
    108.         //magic defense
    109.         GetSkill ((int)SkillName.Magic_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Willpower), .33f));
    110.         GetSkill ((int)SkillName.Magic_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));
    111.         //ranged offense
    112.         GetSkill ((int)SkillName.Ranged_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
    113.         GetSkill ((int)SkillName.Ranged_Offense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Intelligence), .33f));
    114.         //ranged defense
    115.         GetSkill ((int)SkillName.Ranged_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Dexterity), .33f));
    116.         GetSkill ((int)SkillName.Ranged_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Constitution), .33f));
    117.  
    118.     }
    119.  
    120.     public void StatUpdate() {
    121.         for (int cnt = 0; cnt < _vital.Length; cnt++)
    122.             _vital [cnt].Update ();
    123.  
    124.         for (int cnt = 0; cnt < _vital.Length; cnt++)
    125.             _skill [cnt].Update ();
    126.     }
    127.  
    128.  
    129. }
    130.  
     
    Last edited: May 22, 2014
  15. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    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...

    Code (csharp):
    1.    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

    Code (csharp):
    1. _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.
     
  16. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    I edited the post to include the BaseCharacter code, just in case. Will add a new post with PlayerCharacter code.
     
  17. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Code (csharp):
    1.  
    2. public class PlayerCharacter : BaseCharacter {
    3.     // Use this for initialization
    4.     void Start () {
    5.     }
    6.     // Update is called once per frame
    7.     void Update () {
    8.     }
    9. }
    10.  
     
  18. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    CharacterGenerator.Start

    Code (csharp):
    1.         _toon = new PlayerCharacter{};
    should be

    Code (csharp):
    1.         _toon = new PlayerCharacter();
    use ( ) not { }

    might be the issue
     
  19. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    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)
     
  20. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    The offending line.

    Code (csharp):
    1. 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.

    Code (csharp):
    1.  
    2. Vital vit = GetVital ((int)VitalName.Energy);
    3. Attribute att = GetPrimaryAttribute ((int)AttributeName.Endurance);
    4. ModifyingAttribute mod = new ModifyingAttribute (att, 1f);
    5. vit.AddModifier (mod);
    6.  
    See which line is truly throwing the error.
     
  21. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    I assume you mean to replace that one line with those four.

    By doing so and running the debug on the compiler, I get one error. Unity3D gives me three errors.

    MONODEV
    The name 'lf' does not exist in the current context

    UNITY3D
    Assets/Scripts/Character Classes/BaseCharacter.cs(93,71): error CS0103: The name `lf' does not exist in the current context
    Assets/Scripts/Character Classes/BaseCharacter.cs(93,74): error CS1502: The best overloaded method match for `ModifyingAttribute.ModifyingAttribute(Attribute, float)' has some invalid arguments
    Assets/Scripts/Character Classes/BaseCharacter.cs(93,74): error CS1503: Argument `#2' cannot convert `object' expression to type `float'

    It seems that "lf" is the offender. I don't recall putting that in there, unless it's a typo of "of" or "if". It was fairly late last night when I finished...
     
  22. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Replacing 'lf' with 'of' or 'if' gives the same error, but changed accordingly. Removing the term altogether results in an error that reads "'ModifyingAttribute' does not contain a constructor that takes '1' arguments".
     
  23. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    That cures the compiler error, but I'm still left with Unity3D errors.

    NullReferenceException: Object reference not set to an instance of an object
    BaseCharacter.SetupVitalModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:94)
    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)
     
    Last edited: May 22, 2014
  24. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    double click the top null error in the editor, it should take you right to the offending line, who is the culprit?
     
  25. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    In the Unity3D editor, I double-clicked said error. It highlighted the Main Camera, then in the compiler, it set my cursor to...

    vit.AddModifier (mod);
    (line 97)
     
  26. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Double clicking on...

    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)

    Takes me to line 94...

    Code (csharp):
    1.  
    2. GUI.Label (new Rect(OFFSET + STAT_LABEL_WIDTH, statStartingPos + ((cnt + 7) * LINE_HEIGHT), BASEVALUE_LABEL_WIDTH, LINE_HEIGHT), _toon.GetVital(cnt).AdjustedBaseValue.ToString());
    3.  
    Double clicking on...

    NullReferenceException: Object reference not set to an instance of an object
    BaseCharacter.SetupVitalModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:97)
    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)

    Takes me to line 97...

    Code (csharp):
    1.  
    2. vit.AddModifier (mod);
    3.  
    Double clicking on...

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


    Also takes me to line 97...

    Code (csharp):
    1.  
    2. vit.AddModifier (mod);
    3.  
     
  27. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Code (csharp):
    1. vit.AddModifier (mod);
    So either vit is null or mod is null. We know mod isnt null because we define it right above this line, so vit must be null. This means the GetVital function is returning null.

    Code (csharp):
    1.     public Vital GetVital(int index) {
    2.  
    3.         return _vital [index];
    4.  
    5.     }
    This is pretty simple, but there are some things to glean from it, when accessing an array the isnt defined or trying to access an index which is out of bounds you wont just get null, you will get an out of bounds error, so the array exists and hte idex you are trying to get also exists, so the data at the index must = null. Lets check the SetupVital function to see if it is creating null data.

    Code (csharp):
    1.    
    2. private void SetupVitals() {
    3.         for (int cnt = 0; cnt < _vital.Length; cnt++) {
    4.             _vital[cnt] = new Vital();
    5.             SetupVitalModifiers();
    6.         }
    7. }
    8.  
    And here is the problem, when you create the array all values start null. This looks like it fixes it, and it does, but SetupVitalModifiers(); is being called before all indexs are given a value. Which is an issue as SetupVitalModifiers(); attempts to access indexes that are not yet set Except for index 0, which is set before SetupVitalModifiers(); is called.

    The fix
    Code (csharp):
    1.     private void SetupVitals() {
    2.  
    3.         for (int cnt = 0; cnt < _vital.Length; cnt++) {
    4.  
    5.             _vital[cnt] = new Vital();
    6.  
    7.         }
    8.             SetupVitalModifiers();
    9.  
    10.     }
     
  28. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    After applying the fix...

    Line 67 gets...
    Expected class, delegate, enum, interface, or struct
    Code (csharp):
    1.  
    2. private void SetupSkills() {
    3.  
    Line 69 gets that same error twice.
    Code (csharp):
    1.  
    2. _skill[cnt] = new Skill();
    3.  
    Line 72 gets...
    Type or namespace definition, or end-of-file expected
    Code (csharp):
    1.  
    2. }
    3.  
     
  29. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Make sure you didnt accidentally delete a necessary "}"
     
  30. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Okay...
    So now there aren't any errors with the compiler, and my pre-GUI loads correctly. However, nothing in the GUI can be edited, and I'm getting this

    NullReferenceException: Object reference not set to an instance of an object
    CharacterGenerator.DisplaySkills () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:101)
    CharacterGenerator.OnGUI () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:44)

    this

    NullReferenceException: Object reference not set to an instance of an object
    BaseCharacter.SetupSkillModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:120)
    BaseCharacter.SetupSkills () (at Assets/Scripts/Character Classes/BaseCharacter.cs:83)
    BaseCharacter.Awake () (at Assets/Scripts/Character Classes/BaseCharacter.cs:25)
    CharacterGenerator.Start () (at Assets/Scripts/Character Classes/CharacterGenerator.cs:24)

    and this

    NullReferenceException: Object reference not set to an instance of an object
    BaseCharacter.SetupSkillModifiers () (at Assets/Scripts/Character Classes/BaseCharacter.cs:120)
    BaseCharacter.SetupSkills () (at Assets/Scripts/Character Classes/BaseCharacter.cs:83)
    BaseCharacter.Awake () (at Assets/Scripts/Character Classes/BaseCharacter.cs:25)

    Clicking on each one takes me to (respectively)

    this

    Line 101 of CharacterGeneration
    Code (csharp):
    1.  
    2. 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());
    3.  
    this

    Line 120 of BaseCharacter
    Code (csharp):
    1.  
    2. GetSkill ((int)SkillName.Melee_Defense).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.Endurance), .33f));
    3.  
    and this

    [the same as the one directly above]
     
  31. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Check your SetupSkills function, youl find the issue in there. Itl look familiar.
     
  32. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Hallelujah!

    My head was about to explode... from a few simple errors.

    Thank you for the help! The GUI works, the stats are set correctly, and everything's back on track!

    +1 everything you did here. :)

    Wait. Scratch that. It's not working perfectly.

    There's a minor issue with the layout of the GUI. I'll try to attach a screenshot shortly...
     
  33. schragnasher

    schragnasher

    Joined:
    Oct 7, 2012
    Posts:
    117
    Glad i could help. Debugging Null references can send you down some rabbit holes.
     
  34. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
  35. EatUrDemon

    EatUrDemon

    Joined:
    May 19, 2014
    Posts:
    37
    Problem completely solved, the pre-GUI works perfectly!

    Time to continue coding this game... >.>
     
    Last edited: May 22, 2014