Search Unity

So sorry but I have another error with my script for my character creation scene

Discussion in 'Scripting' started by Gwolf-Studios, Dec 22, 2012.

  1. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    Sorry, I know you guys are probably sick of me asking for help but i've run into another error that I have no idea how to fix. The errors are saying:

    Assets/Scripts/Character Classes/CharacterGenerator.cs(25,29): error CS1503: Argument `#2' cannot convert `object' expression to type `string'

    Assets/Scripts/Character Classes/CharacterGenerator.cs(25,29): error CS1502: The best overloaded method match for `UnityEngine.GUI.Label(UnityEngine.Rect, string)' has some invalid arguments

    Assets/Scripts/Character Classes/CharacterGenerator.cs(25,81): error CS1061: Type `PlayerCharacter' does not contain a definition for `GetPrimaryAttribute' and no extension method `GetPrimaryAttribute' of type `PlayerCharacter' could be found (are you missing a using directive or an assembly reference?)

    and here is my script:

    Code (csharp):
    1.  
    2. public class CharacterGenerator : MonoBehaviour {
    3.     private PlayerCharacter _toon;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.        _toon = new PlayerCharacter();
    8.         _toon.Awake();
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.     }
    15.    
    16.     void OnGUI() {
    17.         GUI.Label(new Rect(10, 10, 50, 25), "Name");
    18.         _toon.name = GUI.TextArea(new Rect(65, 10, 100, 25), _toon.name);
    19.        
    20.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    21.             GUI.Label(new Rect(10, 40 + (cnt * 25), 100, 25), ((AttributeName)cnt).ToString());
    22.             GUI.Label(new Rect(115, 40 + (cnt * 25), 30, 25), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
    23.         }
    24.     }
    25. }
    26.  
    It's not finished yet by the way.

    I know by now you guys would be saying "learn C# script!" but I just can't grasp even the basics of it. (my brain only seems to like to learn about stuff I don't need at the moment)
     
  2. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    From your other post it looks like GetPrimaryAttribute is a member of BaseCharacter not PlayerCharacter.
    There are two things you can do to fix this. Either implement GetPrimaryAttribute for PlayerCharacter or make PlayerCharacter inherit from BaseCharacter. My guess is you would want to do the latter.
    To do this, the first line of the PlayerCharacter definition should look like:
    Code (csharp):
    1.  
    2. public class PlayerCharacter : BaseCharacter {
    3.  
     
  3. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    Still the same 3 errors
     
  4. Gibbonator

    Gibbonator

    Joined:
    Jul 27, 2012
    Posts:
    204
    Can you post the definitions of PlayerCharacter and BaseCharacter.
     
  5. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    Uhh...

    Code (csharp):
    1.    
    2.     public string name = "Name";
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.    
    7.     }
    8.    
    9.     // Update is called once per frame
    10.     void Update () {
    11.    
    12.     }
    13.    
    14.     public void Awake()
    15. {
    16.  
    17. }
    18.    
    19. }
    20.  
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;               //added to access the enum class
    4.  
    5. public class BaseCharacter : MonoBehaviour {
    6.     private string _name;
    7.     private int _level;
    8.     private uint _freeExp;
    9.    
    10.     private Attribute[] _primaryAttribute;
    11.     private Vital[] _vital;
    12.     private Skill[] _skill;
    13.    
    14.     public void Awake() {
    15.         _name = string.Empty;
    16.         _level = 0;
    17.         _freeExp = 0;
    18.        
    19.         _primaryAttribute = new Attribute[Enum.GetValues(typeof(AttributeName)).Length];
    20.         _vital = new Vital[Enum.GetValues(typeof(VitalName)).Length];
    21.         _skill = new Skill[Enum.GetValues(typeof(VitalName)).Length];
    22.        
    23.         SetupPrimaryAttributes();
    24.         SetupVitals();
    25.         SetupSkills();
    26.        
    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.    
    51.     // take avg of all players skills and assign that as the player level
    52.     public void CalculateLevel() {
    53.     }
    54.    
    55.    
    56.     private void SetupPrimaryAttributes() {
    57.         for(int cnt = 0; cnt < _primaryAttribute.Length; cnt ++) {
    58.             _primaryAttribute[cnt] = new Attribute();
    59.         }
    60.     }
    61.    
    62.     private void SetupVitals() {
    63.             for(int cnt = 0; cnt < _vital.Length; cnt ++) {
    64.             _vital[cnt] = new Vital();
    65.         }
    66.     }
    67.    
    68.     private void SetupSkills() {
    69.         for(int cnt = 0; cnt < _skill.Length; cnt ++) {
    70.             _skill[cnt] = new Skill();
    71.         }
    72.     }
    73.    
    74.     public Attribute GetPrimaryAttribute(int index) {
    75.         return _primaryAttribute[index];
    76. }
    77.    
    78.         public Vital GetVital(int index) {
    79.         return _vital[index];
    80.     }
    81.    
    82.         public Skill GetSkill(int index) {
    83.         return _skill[index];
    84.     }
    85.    
    86.     private void SetupVitalModifiers() {
    87.         //health
    88.         ModifyingAttribute health = new ModifyingAttribute();
    89.         health.attribute = GetPrimaryAttribute((int)AttributeName.Constitution);
    90.         health.ratio = .5f;
    91.        
    92.         GetVital((int) VitalName.Health).AddModifier(health);
    93.        
    94.         //energy
    95.         ModifyingAttribute energyModifier = new ModifyingAttribute();
    96.         energyModifier.attribute = GetPrimaryAttribute((int)AttributeName.Constitution);
    97.         energyModifier.ratio = 1;
    98.        
    99.         GetVital((int) VitalName.Health).AddModifier(energyModifier);
    100.        
    101.         //mana
    102.        ModifyingAttribute manaModifier = new ModifyingAttribute();
    103.         manaModifier.attribute = GetPrimaryAttribute((int)AttributeName.Willpower);
    104.         manaModifier.ratio = 1;
    105.        
    106.         GetVital((int) VitalName.Health).AddModifier(energyModifier);
    107. }
    108.        
    109.    
    110.     private void SetupSkillModifiers() {
    111.         //melee offence
    112.         GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Might), .33f));
    113.         GetSkill((int)SkillName.Melee_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Nimbleness), .33f));
    114.         //melee defence
    115.         GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
    116.         GetSkill((int)SkillName.Melee_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Constitution), .33f));
    117.         //magic offence
    118.         GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
    119.         GetSkill((int)SkillName.Magic_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
    120.         //magic defence
    121.         GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
    122.         GetSkill((int)SkillName.Magic_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Willpower), .33f));
    123.         //ranged offence
    124.         GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Concentration), .33f));
    125.         GetSkill((int)SkillName.Ranged_Offence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
    126.         //ranged defence
    127.         GetSkill((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Speed), .33f));
    128.         GetSkill((int)SkillName.Ranged_Defence).AddModifier(new ModifyingAttribute(GetPrimaryAttribute((int)AttributeName.Nimbleness), .33f));
    129.     }
    130.    
    131.     public void StatUpdate() {
    132.         for(int cnt = 0; cnt < _vital.Length; cnt++)
    133.             _vital[cnt].Update();
    134.        
    135.         for(int cnt = 0; cnt < _skill.Length; cnt++)
    136.             _skill[cnt].Update();
    137.     }
    138. }
    139.  
    140.  
     
  6. Gwolf-Studios

    Gwolf-Studios

    Joined:
    Oct 7, 2012
    Posts:
    30
    sorry,was that the right script? or definition?
     
  7. Goblox

    Goblox

    Joined:
    Nov 3, 2012
    Posts:
    178
    You will need to decide if you intend to code or not. If you insist on using C# you'll have to learn the basics. Gibbonator is right. You didn't include the the whole class in your "uhh" post so I can't see what happened . Your last problems with the missing 'Awake' method and 'name' field were caused by the missing base as well. I tried to correct both of these.

    Code (csharp):
    1.  
    2.   public class PlayerCharacter : BaseCharacter {
    3.  
    4.     // Use this for initialization
    5.  
    6.     void Start () {
    7.  
    8.    
    9.  
    10.     }
    11.  
    12.    
    13.  
    14.     // Update is called once per frame
    15.  
    16.     void Update () {
    17.  
    18.    
    19.  
    20.     }
    21.  
    22. }
    23.  
    Code (csharp):
    1.  
    2. public class CharacterGenerator : MonoBehaviour
    3. {
    4.  
    5.     private PlayerCharacter _toon;
    6.  
    7.  
    8.  
    9.     // Use this for initialization
    10.  
    11.     void Start ()
    12.     {
    13.  
    14.        _toon = new PlayerCharacter();
    15.  
    16.        _toon.Awake();
    17.  
    18.     }
    19.  
    20.    
    21.  
    22.     // Update is called once per frame
    23.  
    24.     void Update () {
    25.  
    26.    
    27.  
    28.     }
    29.  
    30.    
    31.  
    32.     void OnGUI() {
    33.  
    34.         GUI.Label(new Rect(10, 10, 50, 25), "Name");
    35.  
    36.         _toon.Name = GUI.TextArea(new Rect(65, 10, 100, 25), _toon.Name);
    37.  
    38.        
    39.  
    40.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++) {
    41.  
    42.             GUI.Label(new Rect(10, 40 + (cnt * 25), 100, 25), ((AttributeName)cnt).ToString());
    43.  
    44.             GUI.Label(new Rect(115, 40 + (cnt * 25), 30, 25), _toon.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
    45.  
    46.         }
    47.  
    48.     }
    49.  
    50. }
    51.  
     
  8. Goblox

    Goblox

    Joined:
    Nov 3, 2012
    Posts:
    178
    In the interest of teaching a man to fish:

    You really should never have to come to a forum about compiler errors.

    Google the bolded section of your errors (the part after 'error CSXXXX: ') and leave out the member names in quotes that are unique to your code.

    Assets/Scripts/Character Classes/CharacterGenerator.cs(25,81): error CS1061: Type `PlayerCharacter' does not contain a definition for `GetPrimaryAttribute' and no extension method `GetPrimaryAttribute' of type `PlayerCharacter' could be found (are you missing a using directive or an assembly reference?)


    Like so: http://bit.ly/107x3Uo