Search Unity

Issue connecting scripts

Discussion in 'Scripting' started by Paykoman, Nov 21, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys i hv 2 scripts connected, they work almost 100% fine but i hv an issue, in BaseCharacter one i calculate the Vital and Skill Modifier but when i want them to actualize in character creation with the points i give in my status they always show 0 at all the skills and vital has show the image, can anyone help me figure it out wt im doing wrong plz? ty
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;                            //added to acces enum class
    4.  
    5. public class BaseCharacter : MonoBehaviour
    6. {
    7.     private string _name;
    8.     private int _level;
    9.     private uint _freeExp;                //only assign possitive values
    10.  
    11.     private AttributeClass[] _primaryAttribute;
    12.     private VitalClass[] _vital;
    13.     private Skill[] _skill;
    14.  
    15.     public void Awake()
    16.     {
    17.         _name = string.Empty;
    18.         _level = 0;
    19.         _freeExp = 0;
    20.  
    21.         _primaryAttribute = new AttributeClass[Enum.GetValues(typeof(AttributeName)).Length];
    22.         _vital = new VitalClass[Enum.GetValues(typeof(VitalName)).Length];
    23.         _skill = new Skill[Enum.GetValues(typeof(SkillName)).Length];
    24.    
    25.         SetupPrimaryAttributes ();
    26.         SetupVitals ();
    27.         SetupSkills ();
    28.     }
    29.  
    30.     public string Name
    31.     {
    32.         get{return _name;}
    33.         set{_name = value;}
    34.     }
    35.  
    36.     public int Level
    37.     {
    38.         get{return _level;}
    39.         set{_level = value;}
    40.     }
    41.  
    42.     public uint FreeExp
    43.     {
    44.         get{return _freeExp;}
    45.         set{_freeExp = value;}
    46.     }
    47.  
    48.     public void AddExp(uint exp)
    49.     {
    50.         _freeExp += exp;
    51.  
    52.         CalculateLevel ();
    53.     }
    54.  
    55.     //take average of all of the player skills and assign that as the player level
    56.     public void CalculateLevel()
    57.     {
    58.  
    59.     }
    60.  
    61.    
    62.     private void SetupPrimaryAttributes()
    63.     {
    64.         for (int cnt = 0; cnt < _primaryAttribute.Length; cnt++)
    65.         {
    66.             _primaryAttribute[cnt] = new AttributeClass();  
    67.         }
    68.     }
    69.  
    70.     private void SetupVitals()
    71.     {
    72.         for (int cnt = 0; cnt < _vital.Length; cnt++)
    73.             _vital[cnt] = new VitalClass();
    74.            
    75.         SetupVitalModifiers ();
    76.     }
    77.  
    78.     private void SetupSkills()
    79.     {
    80.         for (int cnt = 0; cnt < _skill.Length; cnt++)
    81.             _skill[cnt] = new Skill ();  
    82.  
    83.         SetupSkillModifiers ();
    84.     }
    85.  
    86.     public AttributeClass GetPrimaryAttribute(int index)
    87.     {
    88.         return _primaryAttribute [index];
    89.     }
    90.  
    91.     public VitalClass GetVital(int index)
    92.     {
    93.         return _vital [index];
    94.     }
    95.  
    96.     public Skill GetSkill(int index)
    97.     {
    98.         return _skill [index];
    99.     }
    100.  
    101.     private void SetupVitalModifiers()
    102.     {
    103.         //health
    104.         GetVital ((int)VitalName.HEALTH).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), .5f));
    105.  
    106.         //energy
    107.         GetVital ((int)VitalName.ENERGY).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), 1));
    108.  
    109.         //mana
    110.         GetVital ((int)VitalName.MANA).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), 1));
    111.  
    112.     }
    113.  
    114.     private void SetupSkillModifiers()
    115.     {
    116.         //melee offence
    117.         GetSkill ((int)SkillName.MELEE_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MIGHT), .33f));
    118.         GetSkill ((int)SkillName.MELEE_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), .33f));
    119.         //melee defence
    120.         GetSkill ((int)SkillName.MELEE_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f));
    121.         GetSkill ((int)SkillName.MELEE_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONSTITUTION), .33f));
    122.         //magic offence
    123.         GetSkill ((int)SkillName.MAGIC_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f));
    124.         GetSkill ((int)SkillName.MAGIC_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.WILLPOWER), .33f));
    125.         //mafic defence
    126.         GetSkill ((int)SkillName.MAGIC_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f));
    127.         GetSkill ((int)SkillName.MAGIC_DEFENSE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.WILLPOWER), .33f));
    128.         //ranged offence
    129.         GetSkill ((int)SkillName.RANGED_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.CONCENTRATION), .33f));
    130.         GetSkill ((int)SkillName.RANGED_OFFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f));
    131.         //ranged defence
    132.         GetSkill ((int)SkillName.RANGED_DEFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.SPEED), .33f));
    133.         GetSkill ((int)SkillName.RANGED_DEFENCE).AddModifier (new ModifyingAttribute (GetPrimaryAttribute ((int)AttributeName.MAGIC), .33f));
    134.     }
    135.  
    136.     public void StatUpdate()
    137.     {
    138.         for (int cnt = 0; cnt < _vital.Length; cnt++)
    139.             _vital [cnt].Update ();
    140.  
    141.         for (int cnt = 0; cnt < _skill.Length; cnt++)
    142.             _skill [cnt].Update ();
    143.     }
    144. }
    145.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System;
    4.  
    5. public class CharacterGenerator : MonoBehaviour
    6. {
    7.     private PlayerCharacter _player;
    8.     private const int STARTING_POINTS = 350;
    9.     private const int MIN_SARTING_ATTRIBUTE_VALUE = 10;
    10.     private const int STARTING_VALUE = 50;
    11.     private int pointsLeft;
    12.  
    13.     // Use this for initialization
    14.     public void Start ()
    15.     {
    16.         _player = new PlayerCharacter ();
    17.         _player.Awake ();
    18.  
    19.         pointsLeft = STARTING_POINTS;
    20.  
    21.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)
    22.         {
    23.             _player.GetPrimaryAttribute(cnt).BaseValue = STARTING_VALUE;
    24.             pointsLeft -= (STARTING_VALUE - MIN_SARTING_ATTRIBUTE_VALUE);
    25.         }
    26.  
    27.         _player.StatUpdate ();
    28.     }
    29.    
    30.     // Update is called once per frame
    31.     void Update ()
    32.     {
    33.    
    34.     }
    35.  
    36.     void OnGUI()
    37.     {
    38.         DisplayName ();
    39.         DisplayPointsLeft ();
    40.         DisplayAttributes ();
    41.         DisplayVitals ();
    42.         DisplaySkills ();
    43.     }
    44.  
    45.     private void DisplayName()
    46.     {
    47.         GUI.Label(new Rect(10,10,50,25), "Name:" );
    48.         _player.Name = GUI.TextField (new Rect (65,10,100,25), _player.Name);
    49.     }
    50.  
    51.     private void DisplayAttributes()
    52.     {
    53.         for(int cnt = 0; cnt < Enum.GetValues(typeof(AttributeName)).Length; cnt++)
    54.         {
    55.             GUI.Label (new Rect(10,40+(cnt * 25),120,25), ((AttributeName)cnt).ToString());
    56.             GUI.Label (new Rect(150,40+(cnt * 25),30,25), _player.GetPrimaryAttribute(cnt).AdjustedBaseValue.ToString());
    57.  
    58.             if(GUI.Button(new Rect(170, 40+(cnt * 25),25,25), "-"))
    59.             {
    60.                 if(_player.GetPrimaryAttribute(cnt).BaseValue > MIN_SARTING_ATTRIBUTE_VALUE)
    61.                 {
    62.                     _player.GetPrimaryAttribute(cnt).BaseValue--;
    63.                     pointsLeft++;
    64.                     _player.StatUpdate ();
    65.                 }
    66.             }
    67.  
    68.             if(GUI.Button(new Rect(200, 40+(cnt * 25),25,25), "+"))
    69.             {
    70.                 if(pointsLeft > 0)
    71.                 {
    72.                     _player.GetPrimaryAttribute(cnt).BaseValue++;
    73.                     pointsLeft--;
    74.                     _player.StatUpdate ();
    75.                 }
    76.             }
    77.         }
    78.     }
    79.  
    80.     private void DisplayVitals()
    81.     {
    82.         for(int cnt = 0; cnt < Enum.GetValues(typeof(VitalName)).Length; cnt++)
    83.         {
    84.             GUI.Label (new Rect(10,40+((cnt+7) * 25),120,25), ((VitalName)cnt).ToString());
    85.             GUI.Label (new Rect(150,40+((cnt+7) * 25),30,25), _player.GetVital(cnt).AdjustedBaseValue.ToString());
    86.         }
    87.     }
    88.  
    89.     private void DisplaySkills()
    90.     {
    91.         for(int cnt = 0; cnt < Enum.GetValues(typeof(SkillName)).Length; cnt++)
    92.         {
    93.             GUI.Label (new Rect(250,40+(cnt * 25),130,25), ((SkillName)cnt).ToString());
    94.             GUI.Label (new Rect(400,40+(cnt * 25),30,25), _player.GetSkill(cnt).AdjustedBaseValue.ToString());
    95.         }
    96.     }
    97.  
    98.     private void DisplayPointsLeft()
    99.     {
    100.         GUI.Label(new Rect(250,10,100,25), "Points Left: " + pointsLeft.ToString());
    101.     }
    102. }
    103.  
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    If PlayerCharacter, VitalClass, AttributeClass, etc., are MonoBehaviours, you can't create them with new. Use AddComponent<T>(). Or, better yet, add them in the inspector at design time and use GetComponent<T>() to find them.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Yap but queston is not that warning... the question is the Health Energy Mana and right status should go up with up of main status ex: Constitution increase health but they keep at 0... same for all the others. increase main status should increase the others in %

    SetupVitalModifiers();
    SetupSkilModifiers();
     
  4. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    The reason why I mentioned this is because if they're non-MonoBehaviour subclasses, the data will not be serialized unless you mark them with the [System.Serializable] attribute.

    If that's not the issue, run through SetupVitals() step by step in MonoDevelop's debugger. Here's a tutorial. You can add watches or examine the locals to determine where it's failing to set the values you expect.
     
    Paykoman likes this.
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok m8 ty i will check it Appreciate
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    seems the code has no errors i will go for serialization process and see if i get it to work.. maybe could be a function called out of place too?
     
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The serialization dont fix the issue... I dont know where im failing this... i re-chack the code 5 times and i dont get it...
     
  8. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Step through the debugger, watching the values in MonoDevelop to make sure they change correctly where you expect them to. If they appear to be correct in SetupSkillModifiers(), then maybe the problem is in DisplaySkills(). Step through the debugger here, too, and make sure that this is returning the correct value:
    Code (csharp):
    1. _player.GetSkill(cnt).AdjustedBaseValue
     
    Paykoman likes this.
  9. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Omfg.. i found it without debugg... When i use AdjustedBaseValue is AdjustBaseValue... i hv bouth and i trade them and call them wrongly..... Ty m8.

    Can i ask u one thing? U know a good place to learn c# any kind of book or something i really need improve a lot of my skills...

    Again ty for ur preciouse help.
    _O_
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,697
    Happy to help. Just practice a lot and look at other people's code. Some code is well-written, some isn't. You can learn from bad code, too. You just learn what not to do. There's a free C# tutorial series here. Regardless of language, it's good to read a lot about software engineering principles. Code Complete is a good book for that, as well as Design Patterns. There's also a different design patterns book for C#, but I recommend the original book. And have fun! :)
     
    Paykoman likes this.
  11. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I will go for it... really need learn this to do it at least well.. not perfect but well for now ;) Appreciate ur time with me m8 (y)
     
  12. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Go watch the videos over again is what you should do.